Click Here to Download FactoryPatternSampleWithCSharp.zip C# Example.
Click Here to Download FactoryPatternSampleWithVBNET.zip VB.Net Example.
Factory Design Pattern is type of creational patterns. We can architect software with the help of factory design pattern. With the help of factory pattern we can centralize creations of objects and also reduce object creation logic at the client side.
Let's see some example on this.
We take booklet display example. You have functionality like display booklet on screen in two ways one is with header and footer and other is plain without header and footer.
In traditional approach you might take two classes "BookletWithHeaderFooter" and "BookletPlain" and create objects depending on your requirement on client side like below code.
But issue occurred when there is another booklet type introduce like display booklet with only header at that time we need to add another case in client side need to add that new object creation logic at client side and again rebuild client side code. We can overcome this issue using factory pattern.
In this pattern we introduced a common interface "IBooklet" and both concrete class "BookletWithHeaderFooter" and "BookletPlain" inherit and implement "IBooklet" interface. So that client references only the "IBooklet" interface that means there is no connection between client and concrete classes. So that if we add new concrete class for booklet we do not need to change in client side.
Below diagram is the factory pattern for our example.
Below is the code snippets of how we implement interface in concrete classes.
The object creation is handled by "FactoryBooklet" class. The "GetBooklet" method generate objects for both types of booklet depending on "BookletType". We centralize object creation in "FactoryBooklet" class. Client need to calls "GetBooklet" method of "FactoryBooklet" class which return "IBooklet" interface object so client only refers "IBooklet" interface object. Client is completely disconnected to concrete classes.
Below diagram is for implement factory class and call that class from client.
To know deeply how method and interface call you need to download code and debug that.
Below are the books that you would like :
Click Here to Download FactoryPatternSampleWithVBNET.zip VB.Net Example.
Factory Design Pattern is type of creational patterns. We can architect software with the help of factory design pattern. With the help of factory pattern we can centralize creations of objects and also reduce object creation logic at the client side.
Let's see some example on this.
We take booklet display example. You have functionality like display booklet on screen in two ways one is with header and footer and other is plain without header and footer.
In traditional approach you might take two classes "BookletWithHeaderFooter" and "BookletPlain" and create objects depending on your requirement on client side like below code.
if (BookletType == 1) { objBooklet = new BookletWithHeaderFooter(); } else if (BookletType == 2) { objBooklet = new BookletPlain(); }
But issue occurred when there is another booklet type introduce like display booklet with only header at that time we need to add another case in client side need to add that new object creation logic at client side and again rebuild client side code. We can overcome this issue using factory pattern.
In this pattern we introduced a common interface "IBooklet" and both concrete class "BookletWithHeaderFooter" and "BookletPlain" inherit and implement "IBooklet" interface. So that client references only the "IBooklet" interface that means there is no connection between client and concrete classes. So that if we add new concrete class for booklet we do not need to change in client side.
Below diagram is the factory pattern for our example.
(To view original image , click on image) |
Below is the code snippets of how we implement interface in concrete classes.
(To view original image , click on image) |
The object creation is handled by "FactoryBooklet" class. The "GetBooklet" method generate objects for both types of booklet depending on "BookletType". We centralize object creation in "FactoryBooklet" class. Client need to calls "GetBooklet" method of "FactoryBooklet" class which return "IBooklet" interface object so client only refers "IBooklet" interface object. Client is completely disconnected to concrete classes.
Below diagram is for implement factory class and call that class from client.
(To view original image , click on image) |
To know deeply how method and interface call you need to download code and debug that.
Below are the books that you would like :
No comments:
Post a Comment