内容由B站UP主动力节点产出,本文仅作为学习笔记
原型设计模式 prototype
个人理解,与单例模式对应,每一次使用对象都会生成一个新对象,依赖Cloneable
接口实现。
举例:
class Student implements Cloneable{
String name;
String age;
@Override
public Object clone() throws CloneNotSupportedException{
return super.clone();
}
// Getter & Setter...
}
class Test{
public static void main(String[] args){
Student A = new Student();
A.setName("小明");
A.setAge("18");
// 未使用原型设计模式
String B= new Student();
A.setName("小红");
A.setAge("17");
// 使用原型设计模式
Student B = (Student) A.clone();
A.setName("小红");
A.setAge("17");
}
}