Part-1 Gang of Four (GOF) Software Design Patterns

Gang Of Four Design Patterns

Sample code: https://github.com/nitinmuteja/GOFDesignPatterns/

Design Patterns: Elements of Reusable Object-Oriented Software (1994) written by  Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides is a book on software engineering highlighting the capabilities and pitfalls of object-oriented programming. They have listed down 23 classic software design patterns which are influential even in the current software development environment. The authors are often referred to as the Gang of Four (GoF).


The 23 design patterns have been categorized into 3 verticals:
1. Creational- Deal with object creation mechanisms, trying to create objects in a manner suitable to the situation.
2. Structural- Deal with easing the design by identifying a simple way to realize relationships among entities.
3. Behavioural- Deal with identifying common communication patterns among objects.

Creational Design Patterns 
1. Singleton- The main motive behind this pattern is to provide a single instance of an object to multiple threads trying to get its instance concurrently.

Following is the class diagram of a singleton class


The variable singleton is private as well as the constructor.
The only source to get an instance is the getInstance() method.

2. Factory- The task of instance creation for a particular interface is provided to a central class which is known as a factory. A factory can provide various implementations of a type of object.


The Factory Creator1 implements Creator and is the factory to create objects of the type Product. The factory method provides an instance of the type Product(abstract class). Here Creator1(concrete factory) provides an instance of the Product1(derived) class through the factoryMethod().

3. Abstract Factory- This pattern is similar to the factory but with a change that it clubs multiple factories into one. The multiple implementations could be extracted from the individual methods or from the Type parameter passed to the generic method of the factory.
A sample UML class and sequence diagram for the Abstract Factory design pattern. [8]

For eg: BaseType objectInstantiated=factory.GetInstance<BaseType>(); 


4. Builder- This pattern is used to separate the construction of complex objects from their representation.
The building of separate components is provided by a builder interface and its implementation is provided by the concrete builder classes. The sequence of the building of components is decided by the Director class.




5. Prototype- This pattern is used for cloning objects given that the original object creational process is time/resource consuming and would be an overhead. 





Comments

Post a Comment

Popular posts from this blog

Email Spoofing and Security

Creating a bootable pendrive