Showing posts with label Design Patterns. Show all posts
Showing posts with label Design Patterns. Show all posts

Factory Design pattern

Factory design pattern provides you an interface creating a single object, but let subclass decide which class to instantiate. This pattern is used most frequently when we are having common functionality with different methods for different objects.

In that scenario, we will create an interface with defining common functionality which will be implemented by the classes. A class will be defined which will be used to identify which class to be instantiated for the object.

For example, We have to create reading utility and we are having 3 different kind of files 1st is pdf, 2nd is doc and 3rd is txt file. On all the files we have to perform open operation but way to open the file will be different. In this case we will create an Interface will contain OpenFile Operation. There will be classes for each file type and the classes will be implementing the factory interface(Example is given in C# Code Example.

C# Code Example:

Singleton Design Pettarn - Details Explaination

What is singleton design pattern ?

Singleton design pattern allows programmer to create only one instance for a class for whole life cycle of process. In this case if developer tries to get new instance of class, he/she will get the same instance of the class which was created once.

Way of Implementation 
To implement singleton design pattern we must have to make all the constructor of the class private, so that developers can not create new instance of the class directly (Developer will not have control to create object of class).
There must be a static  method in class which will be called by developer to obtain the singleton object.