This article address: http://www.cnblogs.com/archimedes/p/java-prototype-pattern.html, reprint please indicate source address.
Prototype mode
Use the prototype instance to specify the kind of object to create and create a new object by copying the prototypes.
Overview
The prototype pattern is a mature pattern of starting from an object with a new object of its own state, the key of which is to define an object as a prototype and provide it with a way to replicate itself.
Clone method of Java.lang.Object class
See "Shades of the Java"
Applicability
1. When a system should be independent of its product creation, composition and representation.
2. When the class to instantiate is specified at run time, for example, by dynamic loading.
3. To avoid creating a factory class hierarchy that is parallel to the product class hierarchy.
4. When an instance of a class can have only one of several different state combinations. Building the corresponding number of prototypes and cloning them may be more convenient than manually instantiating the class each time you use the appropriate state.
Participants
1. Prototype declares a clone of its own interface.
2. Concreteprototype implements the operation of a clone itself.
3. The Client makes a prototype clone itself to create a new object.
Structure and use of prototype pattern
The structure of the pattern consists of two roles:
• Abstract Prototypes (Prototype)
• Concrete Prototypes (concrete Prototype)
UML class diagrams for schemas:
Actual combat part
Example 1: Implement a clone interface and then implement a clone of itself and apply it
1. Abstract prototype (Prototype): Prototype.java
Public Interface Prototype { publicthrows
2. Concrete prototype (concrete Prototype) _1: Cubic.java
Public classCubicImplementsPrototype, cloneable{Doublelength, width, height; Cubic (DoubleADoubleBDoublec) {Length=A; Width=b; Height=C; } PublicObject Cloneme ()throwsclonenotsupportedexception{Cubic Object=(Cubic) clone (); returnobject; } }
2. Concrete prototype (concrete Prototype) _2: Goat.java
ImportJava.io.*; Public classGoatImplementsprototype,serializable{StringBuffer color; Public voidSetColor (stringbuffer c) {color=C; } PublicStringBuffer GetColor () {returncolor; } PublicObject Cloneme ()throwsclonenotsupportedexception{Object Object=NULL; Try{bytearrayoutputstream Outone=NewBytearrayoutputstream (); ObjectOutputStream Outtwo=NewObjectOutputStream (Outone); Outtwo.writeobject ( This); Bytearrayinputstream Inone=NewBytearrayinputstream (Outone.tobytearray ()); ObjectInputStream Intwo=NewObjectInputStream (Inone); Object=Intwo.readobject (); } Catch(Exception event) {System.out.println (event); } returnobject; }}
3. Application Application.java
Public classApplication { Public Static voidMain (string[] args) {Cubic Cubic=NewCubic (12, 20, 66); System.out.println ("The length, width and height of the cubic:"); System.out.println (Cubic.length+ "," + Cubic.width + "," +cubic.height); Try{Cubic cubiccopy=(Cubic) cubic.cloneme (); System.out.println ("The length, width and height of the cubiccopy:"); System.out.println (Cubiccopy.length+ "," + Cubiccopy.width + "," +cubiccopy.height); } Catch(Clonenotsupportedexception ex) {} Goat Goat=NewGoat (); Goat.setcolor (NewStringBuffer ("The White goat")); System.out.println ("Goat is" +Goat.getcolor ()); Try{Goat goatcopy=(Goat) goat.cloneme (); System.out.println ("Goatcopy is" +Goatcopy.getcolor ()); System.out.println ("Goatcopy to change his color to black."); Goatcopy.setcolor (NewStringBuffer ("Goat of Black Color")); System.out.println ("Goat is still" +Goat.getcolor ()); System.out.println ("Goatcopy is" +Goatcopy.getcolor ()); } Catch(Clonenotsupportedexception ex) {} }}
Advantages of Prototype mode
• When the cost of creating a new instance of a class is greater, copying an existing instance using prototype mode can improve the efficiency of creating a new instance.
• The state of the current object can be saved dynamically. At run time, you can use object flow to save a copy of the current object at any time.
You may also be interested in:
Java Design Pattern Series:
Java design Pattern 4--builder mode (builder)
Java design mode 3--singleton mode (Singleton)
Java design Pattern 2--abstract Factory mode (Factory)
Java Design pattern (Factory method)
Introduction to Java Design Patterns 0--Design Patterns
Java design mode 5--prototype mode (PROTOTYPE)