原型模式
原型模式(Prototype Pattern),是指原型实例指定创建对象的种类,并且通过复制这些原型创建新的对象。
原型模式主要适用于以下场景:
- 类初始化消耗资源较多
- 使用new生成一个对象需要非常繁琐的过程(数据准备、访问权限等)
- 构造函数比较复杂
- 在循环中产生大量对象
浅拷贝
先创建原型Prototype接口:
/**
* @author ss_419
*/
public interface Prototype {
Prototype clone();
}
创建具体需要克隆的类ConcretePrototypeA:
package org.example.spring.designpattern.prototype.shallow;
import java.util.List;
/**
* TODO
*
* @author ss_419
* @version 1.0
* @date 2023/3/9 09:15
*/
public class ConcretePrototypeA implements Prototype {
private int age;
private String name;
private List hobbies;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List getHobbies() {
return hobbies;
}
public void setHobbies(List hobbies) {
this.hobbies = hobbies;
}
@Override
public Prototype clone() {
ConcretePrototypeA concretePrototypeA = new ConcretePrototypeA();
concretePrototypeA.setAge(this.getAge());
concretePrototypeA.setName(this.getName());
concretePrototypeA.setHobbies(this.getHobbies());
return concretePrototypeA;
}
@Override
public String toString() {
return "ConcretePrototypeA{" +
"age=" + age +
", name='" + name + '\'' +
", hobbies=" + hobbies +
'}';
}
}
创建Client类:
package org.example.spring.designpattern.prototype.shallow;
/**
* TODO
*
* @author ss_419
* @version 1.0
* @date 2023/3/9 09:17
*/
public class Client {
private Prototype prototype;
public Client(Prototype prototype) {
this.prototype = prototype;
}
public Prototype startClone(Prototype concretePrototype){
return (Prototype) concretePrototype.clone();
}
}
测试代码如下:
package org.example.spring.designpattern.prototype.shallow;
import java.util.ArrayList;
import java.util.List;
/**
* TODO
*
* @author ss_419
* @version 1.0
* @date 2023/3/9 09:20
*/
public class PrototypeTest {
public static void main(String[] args) {
ConcretePrototypeA concretePrototypeA = new ConcretePrototypeA();
concretePrototypeA.setAge(18);
concretePrototypeA.setName("ss");
List hobbies = new ArrayList<String>();
hobbies.add("唱");
hobbies.add("跳");
hobbies.add("Rap");
concretePrototypeA.setHobbies(hobbies);
System.out.println(concretePrototypeA);
// 创建client对象,准备开始克隆
Client client = new Client(concretePrototypeA);
ConcretePrototypeA concretePrototypeClone = (ConcretePrototypeA) client.startClone(concretePrototypeA);
System.out.println(concretePrototypeClone);
System.out.println("concretePrototypeA.getHobbies() = " + concretePrototypeA.getHobbies());
System.out.println("concretePrototypeClone.getHobbies() = " + concretePrototypeClone.getHobbies());
System.out.println("对象地址比较:" +(concretePrototypeClone.getHobbies() == concretePrototypeA.getHobbies()));
}
}
浅拷贝只是完整复制了值类型数据,没有赋值引用对象,换言之,所有的引用对象仍然指向原来的对象,显然这不是我们想要的结果。
深拷贝
创建原型猴子类Monkey:
package org.example.spring.designpattern.prototype.deep;
import java.util.Date;
/**
* TODO 深拷贝对象——猴子
标签:Prototype,Spring,hobbies,原型,QiTianDaSheng,new,设计模式,concretePrototypeA,public
From: https://www.cnblogs.com/atwood-pan/p/17197318.html