Prototype原型模式

原型模式就是指clone对象。当我们要克隆一个对象,这个对象需要实现Cloneable接口,并重写object的clone()方法。克隆分为浅克隆和深克隆。针对一个对象中有引用对象的话,引用对象是否克隆,决定了浅克隆还是深克隆。浅克隆只是clone了引用,具体引用指向的对象都是一个。 深克隆的话不仅是克隆的引用,引用所指向的对象也是克隆的。

 

/**
 * 浅克隆
 */
public class MainPrototype {
    public static void main(String[] args) throws CloneNotSupportedException {
        Product p1 = new Product("面包",10D,new Unit("个"));
        Product p2 = (Product) p1.clone();
        System.out.println(p2.getName()+"--"+p2.getPrice()+"--"+p2.getUnit().getName());
        System.out.println(p1.getUnit() == p2.getUnit());
        p1.getUnit().setName("包");
        System.out.println(p2.getUnit().getName());
    }
}

class Product implements Cloneable{
    private String name;
    private Double price;
    private Unit unit;

    public Product(String name, Double price, Unit unit) {
        this.name = name;
        this.price = price;
        this.unit = unit;
    }

    public String getName() {
        return name;
    }

    public Double getPrice() {
        return price;
    }

    public Unit getUnit() {
        return unit;
    }

    public void setUnit(Unit unit) {
        this.unit = unit;
    }

    @Override
    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    @Override
    public String toString() {
        return "Product{" +
                "name='" + name + '\'' +
                ", price=" + price +
                ", unit=" + unit.getName() +
                '}';
    }
}

class Unit{
    private String name;
    public Unit(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
/**
 * 深克隆
 */
public class MainPrototype {
    public static void main(String[] args) throws CloneNotSupportedException {
        Product p1 = new Product("面包",10D,new Unit("个"));
        Product p2 = (Product) p1.clone();
        System.out.println(p2.getName()+"--"+p2.getPrice()+"--"+p2.getUnit().getName());
        System.out.println(p1.getUnit() == p2.getUnit());
        p1.getUnit().setName("包");
        System.out.println(p2.getUnit().getName());
    }
}

class Product implements Cloneable{
    private String name;
    private Double price;
    private Unit unit;

    public Product(String name, Double price, Unit unit) {
        this.name = name;
        this.price = price;
        this.unit = unit;
    }

    public String getName() {
        return name;
    }

    public Double getPrice() {
        return price;
    }

    public Unit getUnit() {
        return unit;
    }

    public void setUnit(Unit unit) {
        this.unit = unit;
    }

    @Override
    public Object clone() throws CloneNotSupportedException {
        Product p = (Product)super.clone();
        Unit clone = (Unit) p.getUnit().clone();
        p.setUnit(clone);
        return p;
    }

    @Override
    public String toString() {
        return "Product{" +
                "name='" + name + '\'' +
                ", price=" + price +
                ", unit=" + unit.getName() +
                '}';
    }
}

class Unit implements Cloneable{
    private String name;
    public Unit(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }

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

    @Override
    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值