Many people do not understand very concept of usage of them , most of them understand only singleton pastern .As usage is situation based it is very difficult to teach the proper situation in which one has to implement one .Many time we have to use combination of many design pasterns to tackle the situation .To use the cocktail of such pasterns need much experience and keen mind .But even though design patterns are used and other developers do not understand them then they can not maintain that code .So point is this more intellectual solution could confuse other developed if they are not aware of design patterns and ultimate delay in bug fixing.
What is design pattern and why to use them ?
What is a design pattern?
The design patterns are language-independent strategies for solving common object-oriented design problems.
What are the advantages?
- Reusable foolproof Design , and code
- Time saving as they are tested strategies for particular situation .
- Change extensive and flexible .
- Easy maintenance of code
- Better clean code improve communication between developers who work on it.
What are the disadvantages?
Design Pattern Categories
- Creational Design Pattern
instantiation of object .
Exp. Window window = new Window();
- Structural Design Pattern
- Behavioral Design Pattern
Concept
- It deals with the problem of creating objects without specifying the exact class of object that will be created.
- This return the children classes in form of parent class .
- It uses inheritance .(subclasses + interface )
- Factory class have only one instance .
Example -
Use –
- Flexible way to create object without bothering its actual class.
2. Abstract factory
Concept –
Provide interface for creating families of related or dependent objects without specifying their concrete class.
Concrete factory have only one instance .
Example –
Use –
Abstract factory isolates concrete class to be generated .
Creator don’t have idea which kind of class of object it must create
Return several group of related objects .
3. Builder
Concept –
To build Object part by part than creating whole at a time .
The intention is to abstract steps of construction of objects so that different implementations of these steps can construct different representations of objects.
Example –
Use –
- Let you vary product internal representation
- Isolates code for construction and representation
- Finer control over construction process.
4. Prototype
Concept -
Create new Object by copying or cloning the prototype object.
Reduce the cost of creation
Cloning may use shallow copy / deep copy of cloning
May use serialization for deep copy .
Example –
Use –
- To reduce for creation of complex object .
- There is an object instance in memory almost similar to object wanted .Deep Clone that object and set new few fields and use for further purpose.
Concept
Ensure that class has only one instance and provide global point to access it .
Most of times services and factory methods uses this form .
Example-
Use –
While creating factories for object creation
Creating services like file manager , process manager etc .
7. Lazy initialization/Eager initialization
Concept -
- Create new object on first request for the object .
- Create object before the first request for the object .
Comparison
Lazy | Eager |
public class View{ private int courseCount; private boolean isCounted; public int countNbCourses() { if(!isCounted){ courseCount = countCourses(); } return courseCount; } } | public class View{ private int courseCount; private boolean isCounted; public View { courseCount = countCourses(); } public int countNbCourses() { return courseCount; } } |
Summary of creational patterns
Pattern Name | Generalized Situation |
Factory Pattern | Used to choose and return instance of class in form of super class / interface . This uses inheritance . |
Abstract Factory Pattern | Return one of several group of classes . Return object based on chosen factory . Uses Factory pattern internally . |
Builder Pattern | Assemble number of objects to create new composite object this based on data it represent . Many time objects are assembled using factory . |
Prototype Pattern | Copies or clones existing instance than creating new expensive one . |
Singleton pattern | Ensure there is only one instance of object and provide global access to that point . |
Structural Patterns
- Adapter
- Bridge
- Composite
- Proxy
- Façade
- Decorator
Behavioral Pattern
- Chain of responsibility
- Command
- Interpreter
- Iterator
No comments:
Post a Comment