The prototype mode belongs to the creation mode of the object. By giving a prototype object to indicate the type of all objects created, and then creating more objects of the same type with the method of copying the prototype object.
The prototype model has two forms: simple form and boarding form.
Primitive model patterns in simple form
The simple form UML class diagram is as follows:
The role of prototype mode is
Client Role (CLIENTPRO):
Abstract prototype Role (ProtoType):
Concrete prototype role (CONCRETEPROTOTYPE): Replicated objects
The code is as follows:
Public interface ProtoType { /** * Clone a new object according to itself * @author Shu Yuwei * @time 2015-3-25 pm 08:40:01 * @return */ PublicObjectClone();} Public class ConcreteProtoType1 implements ProtoType { PublicProtoTypeClone(){//Clone yourselfProtoType ProtoType =NewConcreteProtoType1 ();returnPrototype }} Public class ConcreteProtoType2 implements ProtoType { /** * Clone yourself * * PublicObjectClone() {ProtoType ProtoType =NewConcreteProtoType2 ();returnProtoType; }} Public class clientpro { /** * Hold Prototype Object interface * / PrivateProtoType ProtoType;/** * Incoming Required prototype interface * @param protoType */ Public Clientpro(ProtoType ProtoType) { This. ProtoType = ProtoType; } Public void Operation(ProtoType p) {ProtoType Copypt = (ProtoType) p.clone (); }}
the original model mode of the registration form
The UML class diagram is as follows:
The code can be viewed according to the class diagram
two different forms of comparison:
If you need to create less prototype object data and are relatively fixed, you can take the first form, in which case the reference to the prototype object can be saved by the client itself.
If the prototype object data you are creating is not fixed, you can take a second approach. In this case, the client does not save a reference to the prototype object, and the task is given to the Administrator object. Before copying a prototype object, the client can see if the Administrator object already has a prototype that meets the requirements. If so, the object reference can be obtained directly from the Administrator class, and if not, the client needs to replicate the prototype object on its own.
Under what circumstances is prototype mode used?
It is assumed that the product class of a system is dynamically loaded, and that the product class has a certain hierarchical structure. At this time, if the factory model is adopted, the factory class has to have a corresponding hierarchical structure. Once the grade structure of the product class changes, the class structure of the factory will have to change, and it is inconvenient to use the Factory mode for the frequently changing system.
At this point, if you use prototype mode, to each product class with a cloning method, you can avoid the use of Factory mode to bring the fixed hierarchy.
Advantages of prototype mode:
1, allow the dynamic increase or decrease product class
2. Provide simplified creation structure
3, with the ability to dynamically load new functions for an application software
4, the product class does not need to have any implementation to determine the hierarchical structure
Java design Pattern-prototype mode (PROTOTYPE)