目录
    • 在原先sheep类基础上实现cloneable接口,重写clone方法。
    • sheep类
    • 新添的cow类
    • 1.cow类也实现cloneable接口
    • sheep类的clone再添加调用cow的clone
    • 1.cow类实现序列化接口,不必实现cloneable接口了
    • 2.在sheep类实现序列化接口

    引例

    问题:

    现在有一只羊(包含属性:名字dolly、年龄2),需要克隆10只属性完全相同的羊。

    一般解法:

    定义sheep类表示羊,包括构造器、getter()和tostring()。

    public class sheep {
        private string name;
        private int age;
        public sheep(string name, int age) {
            this.name = name;
            this.age = age;
        }
        public string getname() {
            return name;
        }
        public int getage() {
            return age;
        }
        @override
        public string tostring() {
            return "sheep{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }
    

    在客户端实例化多利,然后再根据多利的属性去实例化10只羊。

    public class client {
        public static void main(string[] args) {
            sheep sheepdolly=new sheep("dolly",2);
            sheep sheep1 = new sheep(sheepdolly.getname(), sheepdolly.getage());
            sheep sheep2 = new sheep(sheepdolly.getname(), sheepdolly.getage());
            sheep sheep3 = new sheep(sheepdolly.getname(), sheepdolly.getage());
            //....
            system.out.println(sheep1+",hashcode:"+sheep1.hashcode());
            system.out.println(sheep2+",hashcode:"+sheep2.hashcode());
            system.out.println(sheep3+",hashcode:"+sheep3.hashcode());
            //...
        }
    }
    

    运行结果

    优缺点:

    这种方法是我们首先很容易就能想到的,也是绝大多数人的第一做法。

    但缺点也很明显,每次创建新对象时需要获取原始对象的属性,对象复杂时效率很低;此外不能动态获得对象运行时的状态,若类增减属性需要改动代码。

    下面我们看下原型模式的解法。

    原型模式

    原型模式(prototype pattern)是一种创建型设计模式,允许一个对象再创建另外一个可定制的对象,无需知道如何创建的细节。即用原型实例指定创建对象的种类,并且通过拷贝这些原型,创建新的对象。

    工作原理:将原型对象传给那个要发动创建的对象,这个要发动创建的对象通过请求原型对象拷贝它们自己来实施创建。即用基类object的clone()方法或序列化。

    uml类图:

    • prototype:原型类,声明一个克隆自己的接口
    • concreteprototype: 具体的原型类, 实现一个克隆自己的操作
    • client: 客户端让一个原型对象克隆自己,从而创建一个新的对象

    原型模式又可分为浅拷贝和深拷贝,区别在于对引用数据类型的成员变量的拷贝,小朋友你是否有很多问号? 不急 ,看完这两种方法实现你就懂了。

    浅拷贝

    在原先sheep类基础上实现cloneable接口,重写clone方法。

    public class sheep implements cloneable{
        private string name;
        private int age;
        @override
        protected object clone()  {//克隆该实例,使用默认的clone方法来完成
            sheep sheep = null;
            try {
                sheep = (sheep)super.clone();
            } catch (exception e) {
                system.out.println(e.getmessage());
            }
            return sheep;
        }
        public sheep(string name, int age) {
            this.name = name;
            this.age = age;
        }
        @override
        public string tostring() {
            return "sheep{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }
    

    客户端调用

    public class client {
        public static void main(string[] args) {
            sheep sheepdolly=new sheep("dolly",2);
            sheep sheep1 = (sheep)sheepdolly.clone();
            sheep sheep2 = (sheep)sheepdolly.clone();
            sheep sheep3 = (sheep)sheepdolly.clone();
            //....
            system.out.println("sheep1:"+sheep1+",hashcode:" + sheep1.hashcode());
            system.out.println("sheep2:"+sheep2+",hashcode:" + sheep2.hashcode());
            system.out.println("sheep3:"+sheep3+",hashcode:" + sheep3.hashcode());
            //...
        }
    }
    

    运行结果

    至此,原型模式的浅拷贝也成功克隆了三个对象,但是看进度条发现并不简单。

    现在小羊有了一个朋友小牛,sheep类添加了一个引用属性cow,我们同样再克隆一遍。

    sheep类

    public class sheep implements cloneable{
        private string name;
        private int age;
        public cow friend;//新朋友cow对象,其余不变
        @override
        protected object clone()  {
            sheep sheep = null;
            try {
                sheep = (sheep)super.clone();
            } catch (exception e) {
                system.out.println(e.getmessage());
            }
            return sheep;
        }
        public sheep(string name, int age) {
            this.name = name;
            this.age = age;
        }
        @override
        public string tostring() {
            return "sheep{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }
    

    新添的cow类

    public class cow {
        private string name;
        private int age;
        public cow(string name, int age) {
            this.name = name;
            this.age = age;
        }
        @override
        public string tostring() {
            return "cow{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }
    

    客户端调用克隆

    public class client {
        public static void main(string[] args) {
            sheep sheepdolly=new sheep("dolly",2);
            sheepdolly.friend=new cow("tom",1); //并实例化朋友
            sheep sheep1 = (sheep)sheepdolly.clone();
            sheep sheep2 = (sheep)sheepdolly.clone();
            sheep sheep3 = (sheep)sheepdolly.clone();
            //....
            system.out.println("sheep1:"+sheep1+",hashcode:" + sheep1.hashcode());
            system.out.println("sheep1.friend:"+sheep1.friend+",hashcode:" + sheep1.friend.hashcode()+'\n');
            system.out.println("sheep2:"+sheep2+",hashcode:" + sheep2.hashcode());
            system.out.println("sheep2.friend:"+sheep2.friend+",hashcode:" + sheep2.friend.hashcode()+'\n');
            system.out.println("sheep3:"+sheep3+",hashcode:" + sheep3.hashcode());
            system.out.println("sheep3.friend:"+sheep3.friend+",hashcode:" + sheep3.friend.hashcode()+'\n');
            //...
        }
    }
    

    运行结果

    通过运行结果发现,浅拷贝通过object的clone()成功克隆实例化了三个新对象,但是并没有克隆实例化对象中的引用属性,也就是没有克隆friend对象(禁止套娃 ),三个新克隆对象的friend还是指向原克隆前的friend,即同一个对象。

    这样的话,他们四个的friend是引用同一个,若一个对象修改了friend属性,势必会影响其他三个对象的该成员变量值。

    小结:

    • 浅拷贝是使用默认的 clone()方法来实现
    • 基本数据类型的成员变量,浅拷贝会直接进行值传递(复制属性值给新对象)。
    • 引用数据类型的成员变量,浅拷贝会进行引用传递(复制引用值(内存地址)给新对象)。

    深拷贝

    方法一:

    机灵的人儿看出,再clone一遍cow不就好了,但是手动递归下去不推荐。

    1.cow类也实现cloneable接口

    public class cow implements cloneable{
        private string name;
        private int age;
        public cow(string name, int age) {
            this.name = name;
            this.age = age;
        }
        //无引用类型,直接clone即可
        @override
        protected object clone() throws clonenotsupportedexception {
            return super.clone(); //直接抛出了,没用try-catch
        }
        @override
        public string tostring() {
            return "cow{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }
    

    sheep类的clone再添加调用cow的clone

    public class sheep implements cloneable{
        private string name;
        private int age;
        public cow friend;//新朋友cow对象,其余不变
        @override
        protected object clone() throws clonenotsupportedexception {
            object deep = null;
            //完成对基本数据类型(属性)和string的克隆
            deep = super.clone();
            //对引用类型的属性,进行再次clone
            sheep sheep = (sheep)deep;
            sheep.friend  = (cow)friend.clone();
            return sheep;
        }
        public sheep(string name, int age) {
            this.name = name;
            this.age = age;
        }
        @override
        public string tostring() {
            return "sheep{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }
    

    客户端调用

    public class client {
        public static void main(string[] args) throws clonenotsupportedexception {
            sheep sheepdolly=new sheep("dolly",2);
            sheepdolly.friend=new cow("tom",1); //并实例化朋友
            sheep sheep1 = (sheep)sheepdolly.clone();
            sheep sheep2 = (sheep)sheepdolly.clone();
            sheep sheep3 = (sheep)sheepdolly.clone();
            //....
            system.out.println("sheep1:"+sheep1+",hashcode:" + sheep1.hashcode());
            system.out.println("sheep1.friend:"+sheep1.friend+",hashcode:" + sheep1.friend.hashcode()+'\n');
            system.out.println("sheep2:"+sheep2+",hashcode:" + sheep2.hashcode());
            system.out.println("sheep2.friend:"+sheep2.friend+",hashcode:" + sheep2.friend.hashcode()+'\n');
            system.out.println("sheep3:"+sheep3+",hashcode:" + sheep3.hashcode());
            system.out.println("sheep3.friend:"+sheep3.friend+",hashcode:" + sheep3.friend.hashcode()+'\n');
            //...
        }
    }
    

    运行结果

    方法二:

    通过对象序列化实现深拷贝(推荐)

    1.cow类实现序列化接口,不必实现cloneable接口了

    public class cow implements serializable {
        private string name;
        private int age;
        public cow(string name, int age) {
            this.name = name;
            this.age = age;
        }
        @override
        public string tostring() {
            return "cow{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }
    

    2.在sheep类实现序列化接口

    public class sheep implements serializable { //实现序列化接口
        private string name;
        private int age;
        public cow friend;
    
        public sheep(string name, int age) {
            this.name = name;
            this.age = age;
        }
        @override
        public string tostring() {
            return "sheep{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
        public object deepclone() { //深拷贝
            //创建流对象
            bytearrayoutputstream bos = null;
            objectoutputstream oos = null;
            bytearrayinputstream bis = null;
            objectinputstream ois = null;
            try {
                //序列化
                bos = new bytearrayoutputstream();
                oos = new objectoutputstream(bos);
                oos.writeobject(this); //当前这个对象以对象流的方式输出
                //反序列化
                bis = new bytearrayinputstream(bos.tobytearray());
                ois = new objectinputstream(bis);
                sheep sheep = (sheep) ois.readobject();
                return sheep;
            } catch (exception e) {
                e.printstacktrace();
                return null;
            } finally {
                //关闭流
                try {
                    bos.close();
                    oos.close();
                    bis.close();
                    ois.close();
                } catch (exception e2) {
                    system.out.println(e2.getmessage());
                }
            }
        }
    }
    

    3.客户端调用

    public class client {
        public static void main(string[] args) throws clonenotsupportedexception {
            sheep sheepdolly=new sheep("dolly",2);
            sheepdolly.friend=new cow("tom",1); //并实例化朋友
            sheep sheep1 = (sheep)sheepdolly.deepclone();
            sheep sheep2 = (sheep)sheepdolly.deepclone();
            sheep sheep3 = (sheep)sheepdolly.deepclone();
            //....
            system.out.println("sheep1:"+sheep1+",hashcode:" + sheep1.hashcode());
            system.out.println("sheep1.friend:"+sheep1.friend+",hashcode:" + sheep1.friend.hashcode()+'\n');
            system.out.println("sheep2:"+sheep2+",hashcode:" + sheep2.hashcode());
            system.out.println("sheep2.friend:"+sheep2.friend+",hashcode:" + sheep2.friend.hashcode()+'\n');
            system.out.println("sheep3:"+sheep3+",hashcode:" + sheep3.hashcode());
            system.out.println("sheep3.friend:"+sheep3.friend+",hashcode:" + sheep3.friend.hashcode()+'\n');
            //...
        }
    }
    

    运行结果

    原型模式总结:

    • 创建新的对象比较复杂时,可以利用原型模式简化对象的创建过程,同时也能够提高效率
    • 可以不用重新初始化对象,动态地获得对象运行时的状态。
    • 如果原始对象发生变化(增加或者减少属性),其它克隆对象的也会发生相应的变化,无需修改代码
    • 若成员变量无引用类型,浅拷贝clone即可;若引用类型的成员变量很少,可考虑递归实现clone,否则推荐序列化。

    总结

    本篇文章就到这里了,希望能给你带来帮助,也希望您能够多多关注www.887551.com的更多内容!