欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

设计模式--原型模式

程序员文章站 2024-01-20 16:47:40
...

概念

定义

原型模式: 指原用型实例指定创建对象的种类, 并通过复制这些原型创建新的对象

适用场景

  • 构造函数比较复杂的情况下
  • 需要在循环体中产生大量对象时
  • 类初始化消耗的资源较多时
  • 生成对象非常繁琐时

浅克隆

浅克隆克隆的对象,复制的是引用的地址. 也就是说如果我们对任意一个对象的属性值进行修改, 全部克隆对象都会被更改值

原型接口

public interface Prototype {
    Prototype clone();
}

具体需要克隆的对象

public class ConcretePrototypeOne implements Prototype {

    private String name;
    private int age;
    private List hobby;

    public String getName() { return name; }

    public void setName(String name) { this.name = name; }

    public int getAge() { return age; }

    public void setAge(int age) { this.age = age; }


    public List getHobby() { return hobby; }

    public void setHobby(List hobby) { this.hobby = hobby; }

    @Override
    public Prototype clone() {
        ConcretePrototypeOne concretePrototypeOne = new ConcretePrototypeOne();
        concretePrototypeOne.setAge(this.age);
        concretePrototypeOne.setName(this.name);
        concretePrototypeOne.setHobby(this.hobby);
        return concretePrototypeOne;
    }
}

客户端类

public class Client {
    private Prototype prototype;

    public Client(Prototype prototype) {
        this.prototype = prototype;
    }

    public Prototype startClone (Prototype concretePrototype) {
        return (Prototype) concretePrototype.clone();
    }
}

测试类

public class Test {
    public static void main(String[] args) {
        ConcretePrototypeOne concretePrototypeOne = new ConcretePrototypeOne();
        concretePrototypeOne.setName("mcw");
        concretePrototypeOne.setAge(24);
        concretePrototypeOne.setHobby(new ArrayList<String>());
        System.out.println(concretePrototypeOne);

        Client client = new Client(concretePrototypeOne);
        ConcretePrototypeOne concretePrototypeOneClone = (ConcretePrototypeOne)client.startClone(concretePrototypeOne);
        System.out.println(concretePrototypeOneClone);

        List hobbyClone = concretePrototypeOneClone.getHobby();
        List hobby = concretePrototypeOne.getHobby();
        System.out.println("原对象地址值: " + hobby);
        System.out.println("克隆对象地址值: " + hobbyClone);
        System.out.println("地址对比: " + (hobby == hobbyClone));
    }
}

执行结果
设计模式--原型模式

深克隆

深克隆复制的对象,不是同一个引用对象

创建一个代理人类

public class Agent {
    protected BigDecimal agencyFee;
    protected BigDecimal referralFee;
    protected Date createTime;
}

创建引用对象市区代理类

public class CityAgent implements Serializable {
    private BigDecimal af = new BigDecimal(10);
    private BigDecimal rf = new BigDecimal(20);
    public void firstEarnings() {
        this.af = this.af.multiply(new BigDecimal(2));
        this.rf = this.rf.multiply(new BigDecimal(2));
    }

    public void followEarnings() {
        this.af = this.af.multiply(new BigDecimal(0.2));
        this.rf = this.rf.multiply(new BigDecimal(0.2));
    }
}

创建一个执行类

public class ExecuteShare extends Agent implements Cloneable, Serializable {

    public CityAgent cityAgent;

    public ExecuteShare() {
        this.createTime = new Date();
        this.cityAgent = new CityAgent();
    }

    @Override
    protected Object clone () throws CloneNotSupportedException {
        return this.deepClone();
    }

    public Object deepClone(){
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(bos);
            oos.writeObject(this);

            ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
            ObjectInputStream ois = new ObjectInputStream(bis);

            ExecuteShare copy = (ExecuteShare)ois.readObject();
            copy.createTime = new Date();
            return copy;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}

测试类

public class Test {
    public static void main(String[] args) {
        ExecuteShare executeShare = new ExecuteShare();
        try {
            ExecuteShare clone = (ExecuteShare)executeShare.clone();
            System.out.println(" 深克隆: " + (executeShare.cityAgent == clone.cityAgent));
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
    }
}

执行结果:
设计模式--原型模式