当前位置:网站首页>Prototype mode, Baa Baa

Prototype mode, Baa Baa

2022-06-26 05:45:00 Green water monster 12138

What is a prototype pattern

Archetypal model (prototype): Using prototype instances to specify the kind of objects to create , And create new objects by copying these stereotypes

UML chart

 Insert picture description here

Example code

    @Override
    public Prototype clone() {
    
        Prototype clone=null;
        try {
    
             clone = (Prototype) super.clone();
        } catch (CloneNotSupportedException e) {
    
            e.printStackTrace();
        }
        return clone;
    }
public class ConcretePrototype1 extends Prototype{
    
    public ConcretePrototype1(String id) {
    
        super(id);
    }

    @Override
    public Prototype clone() {
    
        return super.clone();
    }
}


public class ConcretePrototype extends Prototype {
    
    public ConcretePrototype(String id) {
    
        super(id);
    }

    @Override
    public Prototype clone() {
    
        return super.clone();
    }
}


It is worth noting that in java Use in clone Methods must implement Clonable Interface , Otherwise it will throw CloneNotSupportedException.
At the same time, we should pay attention to the difference between deep copy and shallow copy , Both shallow and deep copies will open up a space for us to store objects , Shallow copies replicate data of the basic data type , For data of object data type, just copy its pointer , This kind of copy clone Function can help us complete . Deep copy requires us to create our own space to store object data types .
The following is from this website

Use scenario of prototype mode

  • If you need to copy some objects , At the same time, you want the code to be independent of the specific class to which these objects belong , Prototype patterns can be used .

    • This consideration usually occurs when the code needs to deal with objects passed by third-party code through the interface . Even without considering code coupling , Nor can your code rely on the specific classes to which these objects belong , Because you don't know their specific information .
    • The prototype pattern provides a common interface for client code , The client code can interact with all the cloned objects through this interface , It also makes the client code independent of its cloned object concrete classes .
  • If subclasses differ only in how their objects are initialized , Then you can use this pattern to reduce the number of subclasses . Others may create these subclasses to create specific types of objects .

    • In prototype mode , You can use a series of pre generated 、 Various types of objects as prototypes .
      The client does not have to instantiate subclasses according to requirements , Just find the right prototype and clone it .

Relationship with other design patterns

  • In the early stages of many design work Factory method Pattern ( It's simpler , And it's easier to customize through subclasses ), Then evolved to use Abstract factory Pattern 、 Prototype mode or builder Pattern ( More flexible but more complex ).

  • Abstract factory patterns are usually based on a set of factory methods , But you can also use prototype patterns to generate methods for these classes .

  • Prototypes can be used to keep a history of command patterns .

  • A lot of use Portfolio model and Decoration mode The design of often benefits from the use of prototypes . You can use this pattern to copy complex structures , Instead of rebuilding from scratch .

  • Sometimes prototypes can be used as a simplified version of the memo pattern , The condition is that the state of the object you need to store in the history is relatively simple , No need to link to other external resources , Or links can be easily rebuilt .

  • Abstract factory 、 Both the builder and the prototype can be implemented in singleton mode .

原网站

版权声明
本文为[Green water monster 12138]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206260536205697.html