原型模式-浅克隆/深克隆2(4)

1.原型模式:对已有对象进行拷贝,降低消耗。

将对象A拷贝为对象A1、A2.。。。

2.浅拷贝:引用地址不变。(*String是常量,存放在常量区)

package Creating.pratice2;

public class ProtoType {

    public static void main(String[] args) {
        
        Tree tree = new Tree();
        tree.setBranch("主干");
        tree.setLeaf("好多");
        BridNest bridNest = new BridNest();
        bridNest.setKind("big");
        tree.setBridNest(bridNest);
        Tree treeC1 = tree.clone();
        System.out.println(treeC1.getBranch());
        tree.setYear(3);
        tree.setLeaf("多极了");
        System.out.println(treeC1.getYear());
        System.out.println(tree.getYear());
        System.out.println(tree.getLeaf());
        System.out.println(treeC1.getLeaf());
        System.out.println(tree.getBridNest().getKind());
        System.out.println(treeC1.getBridNest().getKind());
        
        bridNest.setKind("big1");
        tree.setBridNest(bridNest);
        System.out.println(tree.getBridNest().getKind());
        System.out.println(treeC1.getBridNest().getKind());
        
        treeC1.getBridNest().setKind("big2");
        System.out.println(tree.getBridNest().getKind());
        System.out.println(treeC1.getBridNest().getKind());
    }

}

class Tree implements Cloneable{
    private String branch;
    private String leaf;
    private int year;
    private BridNest bridNest;
    
    public String getBranch() {
        return branch;
    }
    public void setBranch(String branch) {
        this.branch = branch;
    }
    public String getLeaf() {
        return leaf;
    }
    public void setLeaf(String leaf) {
        this.leaf = leaf;
    }
    public int getYear() {
        return year;
    }
    public void setYear(int year) {
        this.year = year;
    }
    public BridNest getBridNest() {
        return bridNest;
    }
    public void setBridNest(BridNest bridNest) {
        this.bridNest = bridNest;
    }
    @Override
    protected Tree clone(){
        Tree tree = null;
        try {
            tree = (Tree) super.clone();
        }catch (CloneNotSupportedException e){
            e.printStackTrace();
        }
        return tree;
    }
    
    @Override
    public String toString() {
        return "Tree [branch=" + branch + ", leaf=" + leaf + ", year=" + year
                + ", bridNest=" + bridNest + "]";
    }
    
}

class BridNest{
    private String kind;
    private String name;
    private int amount;
    public String getKind() {
        return kind;
    }
    public void setKind(String kind) {
        this.kind = kind;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAmount() {
        return amount;
    }
    public void setAmount(int amount) {
        this.amount = amount;
    }
    
}

 

2.深克隆:完全不一样的两个对象,引用指向新的地址

//深克隆
    protected Tree deepClone() {
        try {
            // 将对象写入流中
            ByteArrayOutputStream bao = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(bao);
            oos.writeObject(this);
        
            // 将对象从流中取出
            ByteArrayInputStream bis = new ByteArrayInputStream(bao.toByteArray());
            ObjectInputStream ois = new ObjectInputStream(bis);
            return (Tree) ois.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
     }
原文地址:https://www.cnblogs.com/zzlcome/p/11314858.html