Design Pattern:
Some authors divided Design patterns into there types creational, structural and behavioral.
Creational patterns are ones that create objects for you, rather than having you instantiate objects directly. This gives your program more flexibility in deciding which objects need to be created for a given case.
Structural patterns help you compose groups of objects into larger structures, such as complex users interfaces or accounting data.
Behavioral patterns help you define the communication between objects in your system and how the flow is controlled in a complex program.
Why we use different design patterns? The fundamental reason for using varies design patterns is to keep classes separated and prevent them from having to know too much about one another. There are a number of strategies that OO programmers to achieve this separation, among them encapsulation and inheritance.
There are several rules in design patterns.
Firstly, program to an interface and not to an implementation.
Secondly, favor object composition over inheritance.
Creational patterns:
1.
The Factory Method provides a simple decision making class that returns one of several possible subclasses of an abstract base class depending on the data that are provided.
2.
The Abstract Factory Method provides an interface to create and return one of several families of related objects.
3.
The Builder Pattern separates the construction of a complex object from its representation, so that several different representations can be created depending on the needs of the program.
4.
The Prototype Pattern starts with an initialized and instantiated class and copies or clones it to make new instances rather than creating new instances.
5.
The Singleton Pattern is a class of which there can be no more than one instance.
When to Use a Factory Pattern
You should consider using a Factory pattern when
1)
A class can’t anticipate which kind of class of objects it must create.
2)
The base class contains default methods and is only subclassed for cases the default methods are insufficient.
3)
Parameters are passed to the factory telling it which of several class types to return. In this case the classes may share the same method names but may do something quite different.