原型模式详解
实现一个原型接口,该接口通过拷贝原型对象创建新的对象。
应用场景
- Java Object clone方法
- 对原有对象需要大量复制
例子:
1.首先创建一个实现Cloneable接口的抽象类
public class Car implements Cloneable {
private String brand;
private BigDecimal price;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
/**
* 克隆方法
*
* @return
* @throws CloneNotSupportedException
*/
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
2.复制对象
Car car = new Car();
car.setBrand("大众");
car.setPrice(BigDecimal.valueOf(150000));
System.out.println(car.getBrand());
System.out.println(car.getPrice());
//克隆
Car car1 = (Car) car.clone();
System.out.println(car1.getBrand());
System.out.println(car1.getPrice());
System.out.println(car);
System.out.println(car1);
car1.setBrand("红旗");
car1.setPrice(BigDecimal.valueOf(200000));
System.out.println(car1.getBrand());
System.out.println(car1.getPrice());
输出结果:
大众
150000
大众
150000
dev.service.impl.mode.prototype.Car@74a14482
dev.service.impl.mode.prototype.Car@1540e19d
红旗
200000
深克隆和浅克隆
浅克隆:复制原有的引用。两个对象同一个引用。
深克隆:复制原对象所有变量并含有原有值,复制的是值。两个对象不同引用
浅克隆
public class Car implements Cloneable {
static class Country {
public Country(String name) {
this.name = name;
}
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Country{" +
"name='" + name + '\'' +
'}';
}
}
private String brand;
private BigDecimal price;
private Country country;
public Country getCountry() {
return country;
}
public void setCountry(Country country) {
this.country = country;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
@Override
public String toString() {
return "Car{" +
"brand='" + brand + '\'' +
", price=" + price +
", country=" + country +
'}';
}
/**
* 克隆方法
*
* @return
* @throws CloneNotSupportedException
*/
@Override
protected Object clone() throws CloneNotSupportedException {
Car car = (Car) super.clone();
return car;
}
public static void main(String[] args) throws CloneNotSupportedException {
Car car = new Car();
car.setBrand("大众");
car.setCountry(new Country("德国"));
car.setPrice(BigDecimal.valueOf(150000));
//克隆
Car car1 = (Car) car.clone();
System.out.println(car1.getBrand());
System.out.println(car1.getPrice());
//修改
car1.setBrand("红旗");
car1.setPrice(BigDecimal.valueOf(200000));
car1.getCountry().setName("中国");
System.out.println(car);
System.out.println(car1);
}
}
结果:
大众
150000
Car{brand='大众', price=150000, country=Country{name='中国'}}
Car{brand='红旗', price=200000, country=Country{name='中国'}}
深克隆
public class Car implements Cloneable {
static class Country implements Cloneable{
public Country(String name) {
this.name = name;
}
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Country{" +
"name='" + name + '\'' +
'}';
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
private String brand;
private BigDecimal price;
private Country country;
public Country getCountry() {
return country;
}
public void setCountry(Country country) {
this.country = country;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
@Override
public String toString() {
return "Car{" +
"brand='" + brand + '\'' +
", price=" + price +
", country=" + country +
'}';
}
/**
* 克隆方法
*
* @return
* @throws CloneNotSupportedException
*/
@Override
protected Object clone() throws CloneNotSupportedException {
Car car = (Car) super.clone();
Country country = (Country) car.getCountry().clone();
car.setCountry(country);
return car;
}
public static void main(String[] args) throws CloneNotSupportedException {
Car car = new Car();
car.setBrand("大众");
car.setCountry(new Country("德国"));
car.setPrice(BigDecimal.valueOf(150000));
//克隆
Car car1 = (Car) car.clone();
System.out.println(car1.getBrand());
System.out.println(car1.getPrice());
//修改
car1.setBrand("红旗");
car1.setPrice(BigDecimal.valueOf(200000));
car1.getCountry().setName("中国");
System.out.println(car);
System.out.println(car1);
}
}
结果:
大众
150000
Car{brand='大众', price=150000, country=Country{name='德国'}}
Car{brand='红旗', price=200000, country=Country{name='中国'}}