设计 模式(原型模式)

简介:

用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象 摘自百度

在开发过程中,有时会遇到为一个类创建多个实例的情况,这些实例内部成员往往完全相同或有细微的差异,而且实例的创建开销比较大或者需要输入较多参数,如果能通过复制一个已创建的对象实例来重复创建多个相同的对象,这就可以大大减少创建对象的开销,这个时候就需要原型模式

原型复制对象的方法:

  • 1、浅克隆
    浅复制仅仅复制所考虑的对象,而不复制它所引用的对象。 Object类提供的方法clone只是拷贝本对象,其对象内部的数组、引用对象等都不拷贝,还是指向原生对象的内部元素地址

  • 2、深克隆
    深复制把要复制的对象所引用的对象都复制了一遍

   /* 深复制 */   二进制的写法,需要类序列化 
    public Object deepClone() throws IOException, ClassNotFoundException { 

      /* 写入当前对象的二进制流 */ 
      ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
      ObjectOutputStream oos = new ObjectOutputStream(bos); 
      oos.writeObject(this); 

      /* 读出二进制流产生的新对象 */ 
      ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); 
      ObjectInputStream ois = new ObjectInputStream(bis); 
      return ois.readObject(); 
  }

代码实现

  • 1、潜克隆:
    学生对象:
public class Stundent  implements  Cloneable{
    private String name; //名字
    private int age; //年龄

    private Lesson lesson; //课程

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public Lesson getLesson() {
        return lesson;
    }

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

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

    public void setLesson(Lesson lesson) {
        this.lesson = lesson;
    }

    public Stundent clone(){
        try {
            return (Stundent)super.clone();
        } catch (CloneNotSupportedException e) {
           System.out.println("对象无法复制");
           return null;
        }
    }
}

课程对象:

public class Lesson {
    private String name; //课程名字

    public String getName() {
        return name;
    }

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

测试:

public class StudentTest {
    public static void main(String[] args) {
        Stundent studnet = new Stundent();
        Lesson lesson = new Lesson();

        studnet.setLesson(lesson);
        Stundent cloneStudent = studnet.clone();

        System.out.println(cloneStudent==studnet);

        System.out.println(studnet.getLesson()==cloneStudent.getLesson());

    }

结果:


image.png
  • 2、深复制
    对象:
public class Stundent  implements Serializable{
    private String name; //名字
    private int age; //年龄

    private Lesson lesson; //课程

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public Lesson getLesson() {
        return lesson;
    }

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

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

    public void setLesson(Lesson lesson) {
        this.lesson = lesson;
    }

    public Stundent deepClone() throws IOException, ClassNotFoundException {
        /* 写入当前对象的二进制流 */
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(this);
        /* 读出二进制流产生的新对象 */
        ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bis);
        return (Stundent)ois.readObject();
    }

}

课程对象:

public class Lesson implements Serializable {
    private String name; //课程名字

    public String getName() {
        return name;
    }

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

测试

public class StudentTest {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Stundent studnet = new Stundent();
        Lesson lesson = new Lesson();

        studnet.setLesson(lesson);
        Stundent cloneStudent = studnet.deepClone();

        System.out.println(cloneStudent==studnet);

        System.out.println(studnet.getLesson()==cloneStudent.getLesson());
        

    }
}

结果:


image.png

你可能感兴趣的:(设计 模式(原型模式))