原型模式

原型模式

原型模式的定义

1.定义:用原型实例来创建指定对象的种类,并且通过拷贝这些原型创建新的对象

原型模式其实就是从一个对象再创建另外一个可定制的对象,而且不需要知道任何的创建细节

原型模式的结构图

image-20210316090602263.png

原型模式的实现

代码结构图:二版结构图(验证深拷贝与浅拷贝)

image-20210316095820886.png

调用关系

image-20210316100732315.png

1.新建具体原型类,实现Java.lang.Cloneable接口,并提供相对应的方法,特别注意需要实现Cloneable的clone方法,该方法使原型模式的关键

package org.example.prototype;

/**
 * @author 
 * @date 2021/3/16 9:08
 **/
public class Resume implements Cloneable{

    private String name;
    private  String sex;
    private  String age;
    private String timeArea;
    private String company;

    public Resume(String name){
        this.name=name;
    }

    //设置个人信息
    public  void setPersonalInfo(String sex,String age){
        this.sex=sex;
        this.age=age;
    }
    //设置工作经历
    public void setWorkExperience(String timeArea,String company){
        this.timeArea=timeArea;
        this.company=company;
    }

    public String getName() {
        return name;
    }

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

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getAge() {
        return age;
    }

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

    public String getTimeArea() {
        return timeArea;
    }

    public void setTimeArea(String timeArea) {
        this.timeArea = timeArea;
    }

    public String getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company = company;
    }

    //显示
    public void Display(){
        System.out.println("name="+this.getName()+"age="+this.getAge()+"sex="+this.getSex()+"timeArea="+this.getTimeArea()+"company="+this.getCompany());
    }
    //clone方法是实现原型模式的关键
    @Override
    public Resume clone(){
        Resume resume=null;
        try{
            resume= (Resume) super.clone();
        }catch (Exception e){
            e.printStackTrace();
        }
        return resume;
    }
}

2.新建客户端进行调用

package org.example.prototype;

/**
 * @author 
 * @date 2021/3/16 9:20
 **/
public class Test {

    public static void main(String[] args) {
        Resume resume = new Resume("原型设计模式");
        resume.setPersonalInfo("男","29");
        resume.setWorkExperience("2020","run");
        resume.Display();
        Resume clone = resume.clone();
        clone.setPersonalInfo("女","18");
        clone.setWorkExperience("2021","HurryRun");
        clone.Display();
    }
}

3.运行结果

image-20210316093518471.png

**深拷贝与浅拷贝

  • 深拷贝与浅拷贝。Object类的clone方法只会拷贝对象中的基本的数据类型,对于数组、容器对象、引用对象等都不会拷贝,这就是浅拷贝。如果要实现深拷贝,必须将原型模式中的数组、容器对象、引用对象等另行拷贝。

    因此如果是没有实现Cloneable接口的对象需要自己实现一次如:

package org.example.prototype;

/**
* @author 
* @date 2021/3/16 9:37
**/
public class WorkExperience implements Cloneable{
  private String timeArea;
  private String company;

  public String getTimeArea() {
      return timeArea;
  }

  public void setTimeArea(String timeArea) {
      this.timeArea = timeArea;
  }

  public String getCompany() {
      return company;
  }

  public void setCompany(String company) {
      this.company = company;
  }
  //實現深拷貝
  @Override
  public WorkExperience clone(){
      WorkExperience workExperience=null;
      try{
          workExperience= (WorkExperience) super.clone();
      }catch (Exception e){
          e.printStackTrace();
      }
      return workExperience;
  }
}

如果对象本身已经实现了Cloneable接口只需要克隆时调用一次clone接口即可,如ArrayList

public class Prototype implements Cloneable {
    private ArrayList list = new ArrayList();
    public Prototype clone(){
        Prototype prototype = null;
        try{
            prototype = (Prototype)super.clone();
            prototype.list = (ArrayList) this.list.clone();
        }catch(CloneNotSupportedException e){
            e.printStackTrace();
        }
        return prototype; 
    }
}

原型模式的优缺点

优点

1.使用原型模式创建对象比直接new一个对象在性能上好得多,因为Object类的clone方法是一个本地方法,它直接操作内存中的二进制流,特别是复制大对象时,性能的差别非常的明显.

2.使用原型模式的另一个好处就是简化了对象的创建,使创建对象就像我们在编辑文档时的复制粘贴一样

缺点

使用原型模式复制对象不会调用类的构造器创建对象,不但构造器中的代码无法执行,甚至访问权限都对原型模式无效;

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容