Design Patterns

Factory Pattern:

Factory is the most commonly used design pattern. Factory design patterns creates objects without exposing the details to the client object. This pattern is used to define a runtime interface for creating objects. This pattern let the subclass to decide the type of the object to create.

Why to Use:

The motivational factor to use this pattern is when a class does not know the type p the object to create beforehand. And it needs a subclass to judge the type of object. This pattern provides a unified interface for client to create objects without necessarily knowing the type and implementation details. 

Builder Pattern:

Builder patterns suggests to Construct complex objects using step by step approach from simple object.  This means divide big Object into smaller and multiple objects which are easy to build. Builder pattern provides the separation between representation and construction. To instantiate a complex object in this pattern the client object is only required to pass the type.

Why to Use:

The main motivational factor to use builder pattern is the complex application because the complex application built up from complex classes and objects. And if in such application there is a requirement to separate out the procedure of object creation from the ones who make up the object then the solution is Builder pattern. 


Singleton Pattern:

Singleton pattern ensures that only one object of a class is instantiated at a time and all client objects (who are supposed to access that object) are accessing to that single object. This provide a global entry point to access the singleton object.

Why to Use:

When there is a requirement to have only one instance of a class (for example for a database connection, or a print spooler etc.) and whole application is supposed to access that a single point. This pattern ensures to not to create more than one object of a class.

Adapter Pattern:

The adapter pattern behaves as a link or bridge between two different classes and/or objects. It is similar to an adapter which we use in our real life like the memory card reader is an example of Adapter, similarly, the power socket is another example of adapter.

Why to use:


When there is need to interact two objects/classes which have different interfaces (that means they are of incompatible types). And you want to connect with each other without changing the existing object and classes on such interfaces, in such cases you end up by creating an Adapter who connects with each of the interfaces without modifying their existing implementation.

Template Method:

This pattern provides a predefined standard for a program or algorithm. It provides a standard to an implementation class. The class which is using this template cannot make any changes in the structure of provided template, instead, it does the implementation according to it. This template is same as the template which we use in real life for anything, for example, for applying admission to an institute we fill up an admission form which has a specific template and we need to fill in the details in that. And if we do not provide our admission application in that particular form then we would not get admission so similarly the class who is using the template method has to stay in the boundaries of it.

Why to Use:


To make the programming unified and when it is required to standardized a particular program or implementation.

Comments