|Lesson 2||What is a creational pattern? |
|Objective|| Define creational patterns.|
Creational patterns involve the construction of new objects. However, they rarely use constructors directly.
Rather, a creational pattern often hides the constructors in the classes being created, and provides alternate methods to return instances of the desired class.
The most common reason for using a creational pattern is that you need to change the class that is instantiated to fit the situation.
Clearly, a constructor in a single class is an inadequate method to return instances of possibly different classes.
Almost always the objects returned are instances of some common superclass.
Class creational patterns use inheritance to vary the object being created.
Object creational patterns generally delegate the actual construction to a different object that is responsible for deciding which class is required and invoking the necessary constructor.
Creational design patterns are design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation.
The basic form of object creation could result in design problems or added complexity to the design.
Creational design patterns solve this problem by somehow controlling this object creation.
Creational design patterns are composed of two dominant ideas.
One is encapsulating knowledge about which concrete classes the system use.
Another is hiding how instances of these concrete classes are created and combined.
- Deal with one of the most commonly performed tasks in an OO application, the creation of objects.
- Support a uniform, simple, and controlled mechanism to create objects.
- Allow the encapsulation of the details about what classes are instantiated and how these instances are created.
- Encourage the use of interfaces, which reduces coupling.
When a client object does not know which class to instantiate, it can make use of the factory method to create an instance of an appropriate class from
a class hierarchy or a family of related classes.
The factory method may be designed as part of the client itself or in a separate class.
The class that contains the factory method or any of its subclasses decides on which class to select and how to instantiate it.
||Provides a controlled object creation mechanism to ensure that only one instance of a given class exists.
||Allows the creation of an instance of a class from a suite of related classes without having a client object to specify the actual concrete class to be instantiated.
||Provides a simpler way of creating an object by cloning it from an existing (prototype) object.
||Allows the creation of a complex object by providing the information on only its type and content, keeping the details of the object creation transparent to the client.
This allows the same construction process to produce different representations of the object.