原型模式 java版

抽象原型 Prototype.java
java


public abstract class Prototype implements Cloneable{
    protected String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
                         
    @Override
    protected Object clone() throws CloneNotSupportedException {
        // TODO Auto-generated method stub
        return super.clone();
    }
}



具體原型 ConcretePrototype.javaide


public class ConcretePrototype extends Prototype {
}



測試類 Test.java測試


public class Test {
    public static void main(String[] args) throws CloneNotSupportedException {
               
        Prototype p = new ConcretePrototype();
        p.setName("zhangsan");
        System.out.println(p.getName());
               
        Prototype p2 = (Prototype) p.clone();
        System.out.println(p2.getName());
    }
}


測試結果:this


zhangsan
zhangsan
相關文章
相關標籤/搜索