有了网站怎么写文章,西安市在建工程项目,在什么网站上可以做免费广告,网站建设课程的建议文章目录 一、原型模式1. 概述2. 结构3. 实现4. 案例1.5 使用场景1.6 扩展#xff08;深克隆#xff09; 一、原型模式
1. 概述 用一个已经创建的实例作为原型#xff0c;通过复制该原型对象来创建一个和原型对象相同的新对象。 2. 结构 原型模式包含如下角色#xff1a; … 文章目录 一、原型模式1. 概述2. 结构3. 实现4. 案例1.5 使用场景1.6 扩展深克隆 一、原型模式
1. 概述 用一个已经创建的实例作为原型通过复制该原型对象来创建一个和原型对象相同的新对象。 2. 结构 原型模式包含如下角色 抽象原型类规定了具体原型对象必须实现的 clone() 方法。具体原型类实现抽象原型类的 clone() 方法它是可被复制的对象。访问类使用具体原型类中的 clone() 方法来复制新的对象。 接口类图如下 3. 实现 原型模式的克隆分为浅克隆和深克隆。 浅克隆创建一个新对象新对象的属性和原来对象完全相同对于非基本类型属性仍指向原有属性所指向的对象的内存地址。 深克隆创建一个新对象属性中引用的其他对象也会被克隆不再指向原有对象地址。 Java中的Object类中提供了 clone() 方法来实现浅克隆。 Cloneable 接口是上面的类图中的抽象原型类而实现了Cloneable接口的子实现类就是具体的原型类。 Realizetype具体的原型类 public class Realizetype implements Cloneable {public Realizetype() {System.out.println(具体的原型对象创建完成);}Overridepublic Realizetype clone() throws CloneNotSupportedException {System.out.println(具体原型复制成功);return (Realizetype) super.clone();}
}PrototypeTest测试访问类 public class PrototypeTest {public static void main(String[] args) throws CloneNotSupportedException {//创建一个原型类对象Realizetype realizetype new Realizetype();//调用Realizetype类中的clone方法进行对象的克隆Realizetype clone realizetype.clone();System.out.println(原型对象和克隆出来的是否是同一个对象 (realizetype clone));}
}运行结果为 4. 案例 用原型模式生成“三好学生”奖状 同一学校的“三好学生”奖状除了获奖人姓名不同其他都相同可以使用原型模式复制多个“三好学生”奖状出来然后在修改奖状上的名字即可。 奖状类 public class Citation implements Cloneable {private String name;public void setName(String name) {this.name name;}public String getName() {return (this.name);}public void show() {System.out.println(name 同学在2020学年第一学期中表现优秀被评为三好学生。特发此状);}Overridepublic Citation clone() throws CloneNotSupportedException {return (Citation) super.clone();}
}测试访问类 public class CitationTest {public static void main(String[] args) throws CloneNotSupportedException {Citation c1 new Citation();c1.setName(张三);//复制奖状Citation c2 c1.clone();//将奖状的名字修改李四c2.setName(李四);c1.show();c2.show();}
}运行结果为 1.5 使用场景 对象的创建非常复杂可以使用原型模式快捷的创建对象。性能和安全要求比较高。 1.6 扩展深克隆 将上面的“三好学生”奖状的案例中Citation类的name属性修改为Student类型的属性。 奖状类 public class Citation implements Cloneable {private Student stu;public Student getStu() {return stu;}public void setStu(Student stu) {this.stu stu;}void show() {System.out.println(stu.getName() 同学在2020学年第一学期中表现优秀被评为三好学生。特发此状);}Overridepublic Citation clone() throws CloneNotSupportedException {return (Citation) super.clone();}
}学生类 public class Student {private String name;private String address;public Student(String name, String address) {this.name name;this.address address;}public Student() {}public String getName() {return name;}public void setName(String name) {this.name name;}public String getAddress() {return address;}public void setAddress(String address) {this.address address;}
}测试类 public class CitationTest {public static void main(String[] args) throws CloneNotSupportedException {Citation c1 new Citation();Student stu new Student(张三, 西安);c1.setStu(stu);//复制奖状Citation c2 c1.clone();//获取c2奖状所属学生对象Student stu1 c2.getStu();stu1.setName(李四);//判断stu对象和stu1对象是否是同一个对象System.out.println(stu和stu1是同一个对象 (stu stu1));c1.show();c2.show();}
}运行结果为 说明 stu对象和stu1对象是同一个对象就会产生将stu1对象中name属性值改为“李四”两个Citation奖状对象中显示的都是李四。这就是浅克隆的效果对具体原型类Citation中的引用类型的属性进行引用的复制。这种情况需要使用深克隆而进行深克隆需要使用对象流。 public class CitationTest1 {public static void main(String[] args) throws Exception {Citation c1 new Citation();Student stu new Student(张三, 西安);c1.setStu(stu);//创建对象输出流对象ObjectOutputStream oos new ObjectOutputStream(new FileOutputStream(C:\\Users\\Think\\Desktop\\b.txt));//将c1对象写出到文件中oos.writeObject(c1);oos.close();//创建对象出入流对象ObjectInputStream ois new ObjectInputStream(new FileInputStream(C:\\Users\\Think\\Desktop\\b.txt));//读取对象Citation c2 (Citation) ois.readObject();//获取c2奖状所属学生对象Student stu1 c2.getStu();stu1.setName(李四);//判断stu对象和stu1对象是否是同一个对象System.out.println(stu和stu1是同一个对象 (stu stu1));c1.show();c2.show();}
}运行结果为 注意Citation类和Student类必须实现Serializable接口否则会抛NotSerializableException异常。