Creational Design Patterns: Just as the name suggests these are design patterns which will help us create objects. Usually we use new key word and the class constructor to create an object, creational design pattern would give us a sophisticated way of creating an object.
1) Factory
2) Abstract Factory
3) Builder
4) Singleton
5) Prototype
Structural Design Patterns: These patterns are concerned with how to group together classes and objects together to form larger structures.
1) Adapter
2) Bridge
3) Composite
4) Decorator
5) Facade
6) Flywheel
7) Proxy
Behavioral Design Patterns: The assignment and division of responsibilities between objects is taken care of in behavioral design patterns.
1) Chain of responsibility
2) Command
3) Iterator
4) Mediator
5) Menento
6) Observer
7) State
8) Strategy
9) Template
10) Visitor
Creational Design Patterns: Let us look at each creational design pattern
Factory Design Pattern: This is the most simple design pattern among all the design patterns. Just as the name suggests imagine a factory which produces cars, generally a car factory will have some different types of cars like Honda Civic, Honda Accord, Honda CRV.
Within a factory we have some systems or machines which are designed to make a specific product.
Factory Design pattern is no different. It is a pattern(if written properly) which will give you an object you requested.
For example a classic example of a design pattern is a Shape Class, which will give you a shape you request.
First create an interface which would have common attribute in all your objects you want. For instance engine and some other parts in Honda Cars could be same.
Shape Interface - draw is the common method in our shape factory
Let us now create a Rectangle class, Triangle Class, Eclipse Class - these are analogous to Honda City, Honda Civic, Honda CRV
Rectangle:
Triangle:
Eclipse:
Factory: Now we are creating our factory which will give us what we want, i.e we can ask for Rectangle, Triangle or Eclipse and we would get the same object as we will see next.
Demo: How this Factory Design Pattern Works: Here we create a Shape Factory object and get the shape we want.
Output: The output below shows that we indeed got the objects we needed.
Abstract Factory Design Pattern: Now we know how to make Cars from Factory Design pattern, so we now own a car factory. Do you want to stop here ? I don't want to stop, let us create an Electric car factory now. Now that we have a gas car factory and an electric car factory, I wish I have a magical object which creates any factory I like. Here is Abstract Factory Design Pattern at your service
No comments:
Post a Comment