one. Defines the type of object created with the prototype instance, and creates a new object by copying the prototypes.
Two. Structure
three. Participants
Prototype: Cloning its own interfaces (such as Fruitprototype in code implementations)
Prototypetool: Managing prototype's tool classes, storing prototypes to replicate themselves to data interfaces (such as Fruittool)
Concreteprototype: Implementing a Clone's own operation (such as Conctetefruitprototype)
Client: Test class
Four. Applicability
--many are used to create complex or time-consuming instances, because in this case, copying an existing instance makes the program run more efficiently
--Create equal values, just the same kind of data with different names
--instantiated classes are loaded at run time, such as dynamic loading
--Avoid creating a factory class hierarchy that is parallel to the product class level (that is, avoid creating too many subclasses like a factory method)
Five. Code implementation
Prototype interface: Provide access to prototype copies externally
Package com.wenniuwuren.prototype;
/**
* Prototype Interface
* @author wenniuwuren * */public
interface fruitprototype{public
abstract Fruitprototype clonenew () throws clonenotsupportedexception;
}
Prototype specific implementations:
Package com.wenniuwuren.prototype;
/**
* Prototype specific implementation
* @author Wenniuwuren * * */Public
class Conctetefruitprototype implements Fruitprototype, cloneable{
private String size;
private String color;
Public Conctetefruitprototype (string size, string color) {
this.size = size;
This.color = color;
}
Clone public
Fruitprototype Clonenew () throws Clonenotsupportedexception {
return (Fruitprototype) Super.clone ();
}
The size of the easy print public
void display (String colorname) {
System.out.println (colorname+) is: "+size+" color is: "+color"; c20/>}
}
Prototype Management classes:
Package com.wenniuwuren.prototype;
Import Java.util.HashMap;
/**
* Prototype Management class
* @author wenniuwuren * * */Public
class Fruittool {
private hashmap<string, fruitprototype> fruits = new hashmap<string, fruitprototype> ();
public void put (String key, Fruitprototype fruitprototype) {
fruits.put (key, Fruitprototype);
}
Public Fruitprototype get (String key) {
return fruits.get (key);
}
}
Test class:
Package com.wenniuwuren.prototype;
public class Client {public
static void Main (string[] args) throws clonenotsupportedexception {
Fruittool Fruitt ool = new Fruittool ();
Initialize the size and color of the fruit
fruittool.put ("Apple", New Conctetefruitprototype ("Middle", "Green"));
Fruittool.put ("Watermelon", New Conctetefruitprototype ("Large", "Red"));
Fruittool.put ("Lemon", New Conctetefruitprototype ("Small", "Yellow"));
String fruitname = "Apple";
Conctetefruitprototype Conctetefruitprototype = (conctetefruitprototype) fruittool
. Get (FruitName). CloneNew ();
Conctetefruitprototype.display (fruitname);
Fruitname = "Lemon";
Conctetefruitprototype = (conctetefruitprototype) fruittool.get (
fruitname). Clonenew ();
Conctetefruitprototype.display (Fruitname);
}
}
Operation Result:
Apple's size is: Middle color is: Green
lemon size is: Small color is: Yellow
The code above is using the JDK's own clone () to implement the prototype mode, in which clone () uses a shallow clone.
shallow cloning: only responsible for cloning data passed by value (such as the base data type, string type), without copying the object it refers to, and all references to other objects still point to the original object (such as the custom class Leafreferrence, in Clone () After the Leafreferrence reference has only one copy, changing the pre-replication index and changing the replicated index is a global change, that is, the object reference is not copied, only one copy of the JVM memory model.
deep cloning: the copy of a copy of the previous clone is not copied, and the basic data type and string is a new copy, before and after the copy is independent. (It is late, deep cloning later to fill up)
Resources:
Wikipedia: Prototype mode
Design pattern: The basis of reusable object-oriented software