Fred = new Fred(); //instance of Fred class
However, this simple way couples the way to create an object with your program. It can not satisfy the variation of exact nature of object creation from time to time in the program in many cases. Creational Pattern abstract the creation process to make your program more flexible and general, because your program will not depend on how objects are created and arranged.
It isolate the details of object creation so your code isn’t dependent on what types of objects there are and thus doesn’t have to be changed when you add a new type of object.
The following are individual Creational Patterns which solve different problems when you create objects in a program .
The Singleton Pattern is intent to ensure a class only has one instance, and provide a global point of accessto it.
The Factory Method provides a simple decision making class which returns one of several possible subclasses of an abstract base class depending on data it is provided.
The Abstract Factory Method provides an interface to create and return one of several families of related objects.
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.
The Prototype Pattern starts with an initialized and instantiated class and copies or clones it to make new instances rather than creating new instances.
没有评论:
发表评论