阜蒙县建设小学校官方网站,佛山网络推广seo,黑果云免费虚拟主机,免插件优化wordpress【设计模式】创建型模式之原型模式 文章目录【设计模式】创建型模式之原型模式1.概述2. 构成3. 实现3.1 浅克隆3.2 深克隆1.概述
原型模式(Prototype Pattern)#xff1a;是用于创建重复的对象#xff0c;同时又能保证性能。这种类型的设计模式属于创建型模式#xff0c;它…【设计模式】创建型模式之原型模式 文章目录【设计模式】创建型模式之原型模式1.概述2. 构成3. 实现3.1 浅克隆3.2 深克隆1.概述
原型模式(Prototype Pattern)是用于创建重复的对象同时又能保证性能。这种类型的设计模式属于创建型模式它提供了一种创建对象的最佳方式之一。这种模式是实现了一个原型接口该接口用于创建当前对象的克隆。当直接创建对象的代价比较大时则采用这种模式。
例如一个对象需要在一个高代价的数据库操作之后被创建。我们可以缓存该对象在下一个请求时返回它的克隆在需要的时候更新数据库以此来减少数据库调用。 2. 构成
原型模式包含如下三个角色
抽象原型类规定了具体原型对象必须实现的的 clone() 方法。具体原型类实现抽象原型类的 clone() 方法它是可被复制的对象。访问类使用具体原型类中的 clone() 方法来复制新的对象。 3. 实现
原型模式的克隆分为浅克隆和深克隆
浅克隆创建一个新对象对于基本类型新对象的属性和原来对象的完全相同对于引用类型新对象的属性仍指向原对象属性的内存地址。深克隆创建一个新对象新对象的引用类型也会被克隆不再指向原有对象地址。
java中的Object类中提供了 clone() 方法来实现浅克隆。 Cloneable 是jdk提供的抽象圆形类而实现了 Cloneable 接口的子实现类就是具体的原型类。
Realizetype具体的原型类
public class Realizetype implements Cloneable {public Realizetype() {System.out.println(具体的原型对象创建完成);}Overridepublic Realizetype clone() throws CloneNotSupportedException {System.out.println(具体原型复制成功);return (Realizetype) super.clone();}
}Client测试类
public class Client {public static void main(String[] args) throws CloneNotSupportedException {//创建一个原型类对象Realizetype realizetype new Realizetype();//调用Realizetype的clone方法进行对象克隆Realizetype clone realizetype.clone();System.out.println(原形对象和克隆出来的是否是同一个对象:(realizetypeclone));}
}运行结果如下 3.1 浅克隆
需求
同一学校的“三好学生”奖状除了获奖人姓名不同其他都相同可以使用原型模式复制多个“三好学生”奖状出来然后在修改奖状上的名字即可。
1定义一个学生类
public class Student {private String name;Overridepublic String toString() {return Student{ name name \ };}public String getName() {return name;}public void setName(String name) {this.name name;}
}2定义一个三好学生类
public class Citation implements Cloneable {//三好学生private Student stu;public Student getStu() {return stu;}public void setStu(Student stu) {this.stu stu;}Overridepublic Citation clone() throws CloneNotSupportedException {return (Citation) super.clone();}public void show() {System.out.println(stu.getName() 同学在2020学年第一学期中表现优秀被评为三好学生。特发此状);}
}3测试浅克隆
public class Test {public static void main(String[] args) throws CloneNotSupportedException {//创建原型对象Citation citation new Citation();Student student new Student();student.setName(张三);citation.setStu(student);//浅克隆Citation citation1 citation.clone();Student student1 citation.getStu();student1.setName(李四);System.out.println(student和student1是不是同一个对象(studentstudent1));citation.show();citation1.show();}
}根据浅克隆的定义如果该测试方法的克隆属于浅克隆那么 studentstudent1 必然为 true因为它们指向同一地址。点击运行结果如下 我们发现studentstudent1果然为true所以说这两个引用对象指向同一个地址才会“里斯同学在…”语句才会输出两次。 3.2 深克隆
深拷贝是基于序列化来实现的所以需要克隆的对象都得实现 Serializable 接口我们在学生类和三好学生类中都实现该接口。
编写测试方法如下
public class Test {public static void main(String[] args) throws Exception {//创建原型对象Citation citation new Citation();Student student new Student();student.setName(张三);citation.setStu(student);//深克隆//序列化对象ObjectOutputStream oos new ObjectOutputStream(new FileOutputStream(d:\\a.txt));oos.writeObject(citation);oos.close();//反序列化对象ObjectInputStream ois new ObjectInputStream(new FileInputStream(d:\\a.txt));Citation citation1 (Citation) ois.readObject();Student student1 citation1.getStu();student1.setName(李四);ois.close();System.out.println(student和student1是不是同一个对象(studentstudent1));citation.show();citation1.show();}
}根据深拷贝定义如果该测试方法属于深拷贝那么 studentstudent1 必然为 false。点击运行结果如下