2004-11-24
Prototype mode
Name: prototype mode,
problem:
Suppose you want to make a graphic representation of the five-wire spectrum editor, graphics to indicate a rest, note, etc., a selection panel to control, select a music object, for example, select a rest on the panel, place it on the tab. We define an abstract graphics class for the music elements made of the five-wire spectrum. Define an abstract Tool class for the panel, and also designs the GraphicsTool class for the addition of some graphics tools to the five-wire spectrum.
The problem is, the Graphics class does not know how to create a five-tab spectrum and join into the score. If you create a graphictool class for each music object class, you will generate a large number of subclasses (unreal). In fact, our subclasses can meet the requirements on another class.
solve:
Let the GraphicStool class produce the original class (prototype) new object by copying Garphics, cloning it and can be added to the prototype of the document as a parameter. Each Graphics subclass supports a Clone action, then the GraphicStool class supports the creation of all Graphics subclasses.
effect:
Reduce the number of product objects. You can delete the product at runtime.
Effectively define the behavior of new classes, can be implemented by cloning operations. Dynamic loading classes are allowed in some environments.
Disadvantages: Every class should consider how to implement clone operations, involving superficial and complex clones.
Photo:
Singleton single case mode
Name: Also known as single piece mode
problem:
In many cases, we need to make sure that a certain type is available in an application. For example, print a fake offline system, we submit print tasks to spooling systems instead of each printing to face new print objects, but is queued by the print system.
How to guarantee only one example and easy access?
solve:
Define a constructor, but is a protected constructor, and without constructing parameters.
Define a static member (equivalent to global variable) to save the single-mode object. Define a static method, such as GetInstance, which returns a unique example. This method first looks at whether a static member is pointed to an example. If you do not create one, you will return this static member directly.
effect:
Whenever, it is ensured that an example is obtained and is just a single instance.
Photo:
The above five types of creation modes provide how flexible creation of objects, while facing flexible and changing object requirements.