本质:克隆生成对象
目的:给出一个原型对象,指出要创建的对象类型,通过克隆方法创建更多同类型的对象
实现方式:
若成员变量无引用类型,浅拷贝clone即可;若引用类型的成员变量很少,可考虑递归实现clone,否则推荐序列化。
客户(Client):
提出创建对象的请求
抽象原型(Prototype):
是接口或抽象类,给出所有的具体原型类所需的方法。
具体原型(Concrete Prototype):
是被复制的对象,实现抽象原型类所要求的方法。
原型模式可以替代工厂模式,并且更方便、灵活。
public class WorkExperience implements Cloneable{
private String workDate;
private String company;
public WorkExperience(String workDate, String company) {
super();
this.workDate = workDate;
this.company = company;
}
public String getWorkDate() {
return workDate;
}
public void setWorkDate(String workDate) {
this.workDate = workDate;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public Object clone() {
return this;
}
}
public class Resume implements Cloneable{
private String name;
private String sex;
private String age;
private WorkExperience work;
public Resume(String name) {
super();
this.name = name;
}
public Resume(String name, String sex, String age, WorkExperience work) {
super();
this.name = name;
this.sex = sex;
this.age = age;
this.work = work;
}
public void setWorkExperience(String workDate,String company) {
this.work.setWorkDate(workDate);
this.work.setCompany(company);
}
@Override
public String toString() {
return "Resume [name=" + name + ", sex=" + sex + ", age=" + age + ", work=" + work + "]";
}
public Object clone() throws CloneNotSupportedException {
//super调用的是接口中的clone(),this调用的是自己的clone
Resume obj=(Resume)super.clone();
obj.work=(WorkExperience)work.clone();
return obj;
}
}
public class Test {
public static void main(String[] args) throws CloneNotSupportedException {
// TODO Auto-generated method stub
WorkExperience work=new WorkExperience("2019-2020","XX公司");
Resume r1=new Resume("小李","男","26",work);
System.out.println(r1);
Resume r2=(Resume) r1.clone();
r2.setWorkExperience("2020-2022", "YY公司");
System.out.println(r2);
}
}
创建一系列不同类型的对象并不同的方式对其进行配置。 如果所需对象与预先配置的对象相同, 那么你只需克隆原型即可, 无需新建一个对象。
支持克隆的对象即为原型(该问题中为订单)
浅拷贝:拷贝基本数据类型 如 id,库存量
深拷贝:拷贝引用数据类型 如 产品
重写了java.lang.Cloneable接口中的clone()方法
public class Order implements Cloneable{
private int id;//订单id
private long num;//库存量
private Product product;//产品
private String username;//订购人
public Order(int id, long num, Product product,String username) {
super();
this.id = id;
this.num = num;
this.product = product;
this.username=username;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public long getNum() {
return num;
}
public void setNum(long num) {
this.num = num;
}
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
@Override
public String toString() {
return "Order [id=" + id + ", num=" + num + ", product=" + product + ", username=" + username + "]";
}
protected Object clone() throws CloneNotSupportedException {
Order order=(Order) super.clone();
order.product=(Product)this.product.clone();
return order;
}
}
public class Product {
private String name;//产品名
private int price;//产品价格
public Product(String name, int price) {
super();
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
protected Object clone() {
return this;
}
@Override
public String toString() {
return "[name=" + name + ", price=" + price + "]";
}
}
public class Test {
public static void main(String[] args) throws CloneNotSupportedException {
// TODO Auto-generated method stub
Product product=new Product("咖啡",10);
Order order=new Order(1,200,product);
int id=1;
int num=200;
while(order.getNum()>0) {
Order neworder=(Order)order.clone();
System.out.println(neworder);
order.setNum(num--);
order.setId(id++);
}
}
}
功能:每当订单的预定产品数量超过1000的时候,就需要把订单拆成两份订单来保存。如果拆成两份后还是超过1000,则继续拆分,直到每份产品预订数量不超过1000。
根据业务,目前的订单系统分成两种,一种是个人订单、一种是公司订单。
客户名称、产品对象(ID,Name),订购产品数量。
公司名称、产品对象(ID,Name),订购产品数量。
public class Test {
public static void main(String[] args) throws CloneNotSupportedException {
// TODO Auto-generated method stub
Product product=new Product("咖啡",10);
long num=10200;
Order order=new Order(1,num,product,"公司订单");
int id=1;
long n=0;
while(n1000)
neworder.setNum(1000);
else
neworder.setNum(num-n);
System.out.println(neworder);
n=n+1000;
}
}
}