Dahua デザインパターン - プロトタイプ

6. プロトタイプ

意図

プロトタイプ インスタンスを使用して、作成するオブジェクトのタイプを指定し、このプロトタイプをコピーして新しいオブジェクトを作成します。

クラス図


実装

public abstract class Prototype {
    
    
    abstract Prototype myClone();
}
public class ConcretePrototype extends Prototype {
    
    

    private String filed;

    public ConcretePrototype(String filed) {
    
    
        this.filed = filed;
    }

    @Override
    Prototype myClone() {
    
    
        return new ConcretePrototype(filed);
    }

    @Override
    public String toString() {
    
    
        return filed;
    }
}
public class Client {
    
    
    public static void main(String[] args) {
    
    
        Prototype prototype = new ConcretePrototype("abc");
        Prototype clone = prototype.myClone();
        System.out.println(clone.toString());
    }
}
abc

JDK

おすすめ

転載: blog.csdn.net/weixin_42917352/article/details/131227665