编程语言
首页 > 编程语言> > Java设计模式之原型模式

Java设计模式之原型模式

作者:互联网

基本介绍

注意事项和细节

package com.fjh.prototype;

class Sheep implements Cloneable{
    private String name;
    private int age;
    private String color;

    public Sheep(String name, int age, String color) {
        this.name = name;
        this.age = age;
        this.color = color;
    }

    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 String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    //克隆该实例,使用默认的clone方法来完成
    @Override
    protected Object clone() {
        Sheep sheep = null;
        try{
            sheep = (Sheep) super.clone();
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        return sheep;
    }

    @Override
    public String toString() {
        return "Sheep{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", color='" + color + '\'' +
                '}';
    }
}

public class PrototypeTest {
    public static void main(String[] args){
        Sheep sheep = new Sheep("yeah",5,"white");
        Sheep sheep2 = (Sheep) sheep.clone();
        Sheep sheep3 = (Sheep) sheep.clone();
        Sheep sheep4 = (Sheep) sheep.clone();
        System.out.println(sheep);
        System.out.println(sheep2);
        System.out.println(sheep3);
        System.out.println(sheep4);
    }
}

浅拷贝介绍

package com.fjh.prototype;

class Sheep implements Cloneable{
    private String name;
    private int age;
    private String color;
    public Sheep friend;

    public Sheep(String name, int age, String color) {
        this.name = name;
        this.age = age;
        this.color = color;
    }

    public void setFriend(Sheep friend) {
        this.friend = friend;
    }

    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 String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    //克隆该实例,使用默认的clone方法来完成
    @Override
    protected Object clone() {
        Sheep sheep = null;
        try{
            sheep = (Sheep) super.clone();
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        return sheep;
    }

    @Override
    public String toString() {
        return "Sheep{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", color='" + color + '\'' +
                '}';
    }
}

public class PrototypeTest {
    public static void main(String[] args){
        Sheep sheep = new Sheep("yeah",5,"white");
        sheep.setFriend(new Sheep("geap",4,"black"));
        Sheep sheep2 = (Sheep) sheep.clone();
        Sheep sheep3 = (Sheep) sheep.clone();
        //浅拷贝
        System.out.println(sheep +"------->"+sheep.friend.hashCode()); //Sheep{name='yeah', age=5, color='white'}------->930990596
        System.out.println(sheep2 +"------->"+sheep2.friend.hashCode()); //Sheep{name='yeah', age=5, color='white'}------->930990596
        System.out.println(sheep3 +"------->"+sheep3.friend.hashCode()); //Sheep{name='yeah', age=5, color='white'}------->930990596
    }
}

深拷贝介绍

方式1:重写clone方法实现

package com.fjh.prototype;

class Sheep implements Cloneable{
    private String name;
    private int age;
    private String color;
    public Sheep friend;

    public Sheep(String name, int age, String color) {
        this.name = name;
        this.age = age;
        this.color = color;
    }

    public void setFriend(Sheep friend) {
        this.friend = friend;
    }

    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 String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    @Override
    protected Object clone() {
        Sheep sheep = null;
        try{
            // 这里完成对基本数据类型(属性)和String的克隆
            sheep = (Sheep) super.clone();
            // 对引用类型的属性进行单独处理
            sheep.friend = (Sheep) sheep.friend.clone();
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        return sheep;
    }

    @Override
    public String toString() {
        return "Sheep{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", color='" + color + '\'' +
                '}';
    }
}

public class PrototypeTest {
    public static void main(String[] args){
        Sheep sheep = new Sheep("yeah",5,"white");
        sheep.setFriend(new Sheep("geap",4,"black"));
        Sheep sheep2 = (Sheep) sheep.clone();
        Sheep sheep3 = (Sheep) sheep.clone();
        //深拷贝
        System.out.println(sheep +"------->"+sheep.friend.hashCode()); // Sheep{name='yeah', age=5, color='white'}------->930990596
        System.out.println(sheep2 +"------->"+sheep2.friend.hashCode()); // Sheep{name='yeah', age=5, color='white'}------->1921595561
        System.out.println(sheep3 +"------->"+sheep3.friend.hashCode()); // Sheep{name='yeah', age=5, color='white'}------->565760380
    }
}

方式2:通过序列化实现

package com.fjh.prototype;

import java.io.*;

class Sheep implements Serializable {
    private String name;
    private int age;
    private String color;
    public Sheep friend;

    public Sheep(String name, int age, String color) {
        this.name = name;
        this.age = age;
        this.color = color;
    }

    public void setFriend(Sheep friend) {
        this.friend = friend;
    }

    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 String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public Sheep deepClone(){
        // 创建流对象
        ByteArrayOutputStream bos = null;
        ObjectOutputStream oos = null;
        ByteArrayInputStream bis = null;
        ObjectInputStream ois = null;
        Sheep sheep = null;
        try{
            //序列化
            bos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(bos); // 字节数组输入流转换成对象输出流
            oos.writeObject(this); // 当前这个对象以对象流的方式输出

            //反序列化
            bis = new ByteArrayInputStream(bos.toByteArray());
            ois = new ObjectInputStream(bis);
            sheep = (Sheep) ois.readObject();
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }finally {
            // 关闭流
            try{
                bos.close();
                oos.close();
                bis.close();
                ois.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return sheep;
    }

    @Override
    public String toString() {
        return "Sheep{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", color='" + color + '\'' +
                '}';
    }
}

public class PrototypeTest {
    public static void main(String[] args){
        Sheep sheep = new Sheep("yeah",5,"white");
        sheep.setFriend(new Sheep("geap",4,"black"));
        Sheep sheep2 = sheep.deepClone();
        Sheep sheep3 = sheep.deepClone();
        //浅拷贝
        System.out.println(sheep +"------->"+sheep.friend.hashCode()); // Sheep{name='yeah', age=5, color='white'}------->1268447657
        System.out.println(sheep2 +"------->"+sheep2.friend.hashCode()); // Sheep{name='yeah', age=5, color='white'}------->110718392
        System.out.println(sheep3 +"------->"+sheep3.friend.hashCode()); // Sheep{name='yeah', age=5, color='white'}------->425918570
    }
}

标签:Sheep,Java,name,color,age,原型,设计模式,public,String
来源: https://www.cnblogs.com/isfjh/p/15152673.html