Design Patterns for all – Chapter I (Introduction to Design patterns and OOPs)
In this chapter we will see the basics of Design patterns and OOPs which will give the foundation for understanding Design patterns better. In the coming chapters we will see the Design patterns in detail with practical examples and code.
Design Patterns are best proven techniques for a common design problem.
It is only a design technique and not code. Though code is available for almost all design patterns in all popular languages, design patterns mean the design technique alone.
Each design pattern explains a repeated problem, gives standard solution to the problem.
GoF Gang of four, a four member team (Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides) are said to be people who brought up this idea. They wrote some patterns which are considered the base for all other patterns.
Types of Patterns:
Design Patterns are generally classified into three categories, they are:
They deal with object creation. They provide you the best way to create objects based on the current situation.
Singleton, Factory method, Abstract factory, Builder
They deal with relationship between entities. They provide you the best way to relate entities on various scenarios.
Adapter, Aggregate, Bridge
They deal with communication between objects. They provide you the best way to communicate between objects.
Chain of responsibility, Command, Interpreter
Though the above three are the major classification, one more category also exists which may be considered as a sub part of the above. It is:
They deal with multi-threaded programs. They provide solutions for multi-thread design problems.
Balking, Double checked locking, guarded suspension
OOP Techniques and Design Patterns:
Before going into design patterns, we need to know some of the OOP (Object Oriented Programming) features, which will in turn lead to a better understanding of Design patterns.
Let us now see the OOPs features one by one,
It defines characteristics of a thing and its behavior. It is blueprint from based on which objects are created.
Example: Employee (which will have all the details about the employee like name, id, salary, etc and the methods which describes his behavior like calcSalary(),displaydetails(),etc.
Is an instance of a class. It will be exactly the same as a class but can be assigned values and used whereas a class can’t be used without creating objects.
A feature in which the base class’s characteristics and behavior are derived to the child class also.
A class inherits from a single class.
A class inherits from more than 1 class.
“Abstraction is simplifying complex reality by modeling classes appropriate to the problem, and working at the most appropriate level of inheritance for a given aspect of the problem.” Is what the Wikipedia says.
It is a feature which hides data and functions from others classes.
A feature using which more than one definition is given to a single name. It can either be function polymorphism or operator overloading.
Association defines a relationship between classes of objects which allows one object to cause another to perform an action on its behalf.
Composition is a way to combine simple objects or data types into more complex ones.
Aggregation is a form of composition where one looks at the system as a whole rather than as parts.