Factory Pattern, Abstract Factory Pattern, Singleton Pattern
Factory Pattern
It is responsible for creating the objects. As name depicts, it is a factory for creating any specific kind of objects. It abstracts the user from the details of creation mechanism, and also abstracts the actual implementation class of instantiated object. The only requirement is that the ‘object to create’ should be of some already defined type. The actual implementation may differ based on the current scenario.Example
- Maruti Car Factory Object: We can ask this object to return us a Maruti car based on any runtime parameter which defines its model like Wagon R, or Esteem. It is the responsibility of this factory object to return us required car implementation based on specific parameters.
- Row Renderer Factory Object: We can ask it to return the renderer like structured row renderer, or a designable row renderer.
When to Use
- We can’t decide in advance that which class of object we may require at runtime, however type is known. So the type is known but the implementing class may vary depending upon runtime scenario.
- We want to localize the knowledge of object creation into some class, so that responsibility resides with one object only.
- We want to make the system configurable and extensible and so want to program to interface not to implementation.
Usage
- We are free from object creation and choosing the right implementing class. This responsibility can be owned by the framework, and developers can work to interface following certain guidelines.
- The mechanism for choosing the right implementing class can be made very configurable by using some configuration files (INI or XML).
- Programming to interface rather than to concrete class so enabling extensibility of the system.
Abstract Factory Pattern
It provides an interface to create and return one of the several families of related objects. It can also be referred as factory of factory objects. It is one level higher abstraction than Factory Pattern. It is a factory object which can return one of the several factory objects.
Example
- Car Factory Object: We can ask it for returning as a specific type of car factory object like Maruti Car Factory, Hyundai Car Factory or GM Car Factory. Further the return specific car factories can be used to return cars of different model for respective brands.
- Renderer Factory: We can ask it to return factory object for a specific purpose renderer like Row Renderer, Column Renderer, or Header Renderer Factory. Further these factories can be used to return specific type of renderer of different implementation as defined with Factory Pattern.
- About same as mentioned with factory pattern.
Usage
- It isolates the concrete classes that are generated; due to this it is very easy to change the implementation.
- Rest same as mentioned with factory pattern, however provides higher level of abstraction.
Singleton Pattern
It is a special class, for which we can not have more than one instance in system at a time. It provides a single global point of access to that instance. Hence it ensures that only one instance can be created for this class and every user is forced to use only that single instance for any operation.
Example
- Any Utility Class: Generally utility classes do not have states with these and so a single instance of that class can serve the whole system.
- Memory Manager: An object which is responsible to manage the memory in system. Obviously only there should be only one instance of this object so that it can update the objects with the current memory availability and other objects can update its state for expected memory requirement.
When to Use
- When only one instance can solve the purpose like utility classes, and does not contain any state with it.
- When only one instance is must to preserve and synchronize the entire system states, so to share the state.
- Helps to ensure only one object in memory.
- Helps to share the states across the system.
Difficult to extend any singleton class as it will work only if super class has not been instantiated by now.
0 comments:
Post a Comment