Creational Design Patterns
Creational Design is one of the Design Patterns used with .NET. In this tutorial you will learn about Creational Design Patterns, Factory Method, Abstract Factory, Builder, Prototype and Singleton.
Creational Patterns :
* Factory Method
* Abstract Factory
* Builder
* Prototype
* Singleton
Category : Creational Pattern
Pattern : Factory Design
Brief Description :
This being a ‘creational design pattern’ it focuses on how the objects are created. In the Factory design pattern – the client uses a specialized object solely to create other objects. In the diagram, you would observe that the object (“product”) creation process is handled by the “Factory” object and it is abstracted from the “client” object.
Flow Illustration :
Why would anybody use such a design pattern?
Imagine you are writing the code for the “Client” object. The “client” object needs to use the “Product” object. You hard code the object creation process in the “Client” Now any changes in the object creation procedure would mean that you need to change the “client” too. The sole purpose of the “Factory” object is to create “Product” This gives us a certain amount of flexibility. The “client” delegates that task to “Factory”. Any changes in the object creation process do not affect the “client” and changes to “Factory” should be fairly simple as that is the only thing it does.
Category : Creational Pattern
Pattern : Abstract Factory
Brief Description :
Imagine a bakery that has an oven for baking different types of cakes. The overall process of baking different types of cake is similar and the same oven creates all types of cakes. The only limitation is that the cake should ‘fit’ in the oven. When the oven was created it was created to bake any type of cake/pie/bread/etc that would fit in it. Once you have the oven you can create any of these. The oven does not have the recipe for creating a specific cake. You have the recipe and you supply different ingredients to it and with a few variations/changes to the process different types of cakes are produced. Similarly, The Abstract Factory pattern provides an interface for creating families of related or dependent classes without having to specify the actual classes. The Abstract Factory Design Pattern provides a single interface to create a family of related objects. The family members (“concrete classes”) are specified elsewhere. The Abstract Factory uses “Factory Methods” to create the specific objects. The Factory Methods are in a separate class.
Illustration :
Where it is used?
The Abstract Factory is used to develop frameworks and systems that can be configured with one of multiple families of products (UI controls, menus, toolbars, etc.)
Example using C# :
. class BakedGoodsCreator: Oven class BroiledGoodsCreator: Oven abstract class OvenCooked class Cake:OvenCooked class BroiledChicken:OvenCooked class Bakery
|
Category : Creational Pattern
Design Pattern : Builder
Brief Description :
Highlights of the Builder Design Pattern are:
– This is a Creational Design Pattern and focuses on how the objects are created
– Complex objects are created in parts as needed
– Finally the parts can be put together to obtain the final result or the product
Illustrations :
Class Diagram
Category : Creational Pattern
Design Pattern : Prototype
Brief Description :
Highlights of the Prototype Design Pattern are:
– This is a Creational Design Pattern and focuses on how the objects are created
– An object is instantiated and can be called a prototype
– As needed the above object is cloned and new objects are created
– The new objects are copies of the original one i.e. the protoype
Illustrations :
Category : Creational Pattern
Design Pattern : Singleton
Brief Description :
Highlights of the Singleton Design Pattern are:
– This is a Creational Design Pattern and focuses on how the objects are created
– The first time the object is needed an instance is created
– Further requests to create/access the object are redirected to the original instance
– The Singleton Design Pattern ensures that there exists only one instance of the object
Illustrations :