Briefly talk about design patterns - prototype pattern

1. What is the prototype mode

       Prototype pattern (Prototype), use prototype instances to specify the kind of objects to be created, and create new objects by copying these prototypes. The UML structure diagram is as follows:


       Among them, Prototype is a prototype class that declares an interface that clones itself; ConcretePrototype is a concrete implementation class that implements an operation of cloning itself; and the client Client only needs to clone itself from a prototype to create a new object.

    1. Prototype

public abstract class Prototype implements Cloneable {
	
	private String id;
	
	public Prototype(String id) {
		this.id = id;
	}
	
	public String getId() {
		return id;
	}

	@Override
	public Prototype clone() {
		Prototype prototype = null;
		
		try {
			prototype = (Prototype) super.clone();
		} catch (CloneNotSupportedException e) {
			e.printStackTrace ();
		}
		
		return prototype;
	}
	
}

    2.ConcretePrototype

       Creates a shallow copy of the current object.

public class ConcretePrototype extends Prototype {

	public ConcretePrototype(String id) {
		super(id);
	}
	
}

    3. Client

public class Client {

	public static void main(String[] args) {
		ConcretePrototype p1 = new ConcretePrototype("Hello");
		ConcretePrototype c1 = (ConcretePrototype) p1.clone();
		System.out.println(c1.getId());
	}
	
}

       The result of the operation is "Hello", and the object p1 of ConcretePrototype gets a new instance c1.

Second, the application of the prototype mode

    1. When to use

  • When a system should be created, composed and represented independently of its product.
  • When the class to instantiate is specified at runtime (eg dynamically loaded).
  • To avoid creating a factory class hierarchy parallel to the product class hierarchy.
  • When an instance of a class can only have one of several different state combinations.

    2. Advantages

  • Excellent performance. Instead of reinitializing the object, dynamically obtain the runtime state of the object.
  • Escape the constraints of the constructor.

    3. Disadvantages

  • Configuring the clone method requires a thorough consideration of the functionality of the class.
  • Must implement the Cloneable interface.

    4. Usage scenarios

  • Resource optimization scenarios.
  • Scenarios with performance and security requirements.
  • Scenarios with multiple modifiers for one object.
  • Usually occurs with the factory method pattern, an object is created by the clone method, and then provided to the caller by the factory method.

    5. Application Examples

  • Cell division
  • Object.clone() method in Java
  • copy

Third, the realization of the prototype mode

       Below we create an abstract class and an entity class that extends it, that is, a graphics class with circles, rectangles, and triangles. Objects are then stored in a Hashtable through a cache class and their clones are returned when requested. The UML diagram is as follows:


    1. Shape类

       Create an abstract class that implements the Cloneable interface.

public abstract class Shape implements Cloneable {
	
	private String id;
	protected String type;
	
	public abstract void draw();

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getType() {
		return type;
	}
	
	@Override
	public Shape clone() {
		Shape prototype = null;
		
		try {
			prototype = (Shape) super.clone();
		} catch (CloneNotSupportedException e) {
			e.printStackTrace ();
		}
		
		return prototype;
	}
	
}

    2. Implementation class

       Create an implementation class that extends the above abstract class, taking Circle as an example.

public class Circle extends Shape {

	public Circle() {
		type = "round";
	}
	
	@Override
	public void draw() {
		System.out.println("Draw method of circle class");
	}

}

    3. ShapeCache

       Get the entity class and store it in Hashtable.

public class ShapeCache {

	private static Hashtable<String, Shape> shapeMap = new Hashtable<>();
	
	public static Shape getShape(String shapeId) {
		Shape shape = shapeMap.get(shapeId);
		
		return shape.clone();
	}
	
	//Add three graphics
	public static void loadCache() {
		Circle circle = new Circle();
		circle.setId("1");
		shapeMap.put(circle.getId(), circle);
		
		Triangle triangle = new Triangle();
		triangle.setId("2");
		shapeMap.put(triangle.getId(), triangle);
		
		Rectangle rectangle = new Rectangle();
		rectangle.setId("3");
		shapeMap.put(rectangle.getId(), rectangle);
	}
	
}

    4. Client client

public class Client {

	public static void main(String[] args) {
		ShapeCache.loadCache();
		
		Shape clonedShape = ShapeCache.getShape("1");
		System.out.println("图形:" + clonedShape.getType());
		
		Shape clonedShape2 = ShapeCache.getShape("2");
		System.out.println("图形:" + clonedShape2.getType());
		
		Shape clonedShape3 = ShapeCache.getShape("3");
		System.out.println("图形:" + clonedShape3.getType());
		
	}
	
}

       The results are as follows:



       Source address: https://gitee.com/adamjiangwh/GoF

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325160102&siteId=291194637