原型模式
描述:
原型模式的主要目的是在需要频繁创建重复的对象的时候,针对一个源对象,创建这个源对象的克隆对象。特别是在创建复杂对象时保证了性能。
常用场景:
在创建很复杂,或者创建对象时消耗贼大的场景中可以使用原型模式。
比如创建对象时需要大量的sql语句去赋值,这时,为了避免数据库IO,内存和cpu的消耗就可以使用原型模式。
项目结构:
实现:
1.创建一个实现了Cloneable类的抽象类Mycloth
/**
* @author Carl
* @version 1.0
* @date 2020/8/14 15:34
* @description
**/
public abstract class MyCloth implements Cloneable {
private String name;
private String type;
private String size;
private float price;
public abstract void getMsg();
public String getName(){
return this.name;
}
public void setName(String name){
this.name = name;
}
public String getType() {
return this.type;
}
public void setType(String type){
this.type = type;
}
public String getSize() {
return this.size;
}
public void setSize(String size) {
this.size = size;
}
public float getPrice() {
return this.price;
}
public void setPrice(float price){
this.price = price;
}
@Override
public Object clone(){
Object clone = null;
try {
clone = super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return clone;
}
}
2.创建Pants类,实现MyCloth抽象类
/**
* @author Carl
* @version 1.0
* @date 2020/8/14 15:49
* @description
**/
public class MyPants extends MyCloth {
@Override
public void getMsg() {
System.out.println("Name:"+super.getName()+" "+"Type:"+super.getType()+" "+"Size:"+super.getSize()+" "+super.getPrice());
}
}
3.创建Myshirt类,实现Mycloth抽象类
/**
* @author Carl
* @version 1.0
* @date 2020/8/14 15:39
* @description
**/
public class MyShirt extends MyCloth {
@Override
public void getMsg() {
System.out.println("Name:"+super.getName()+" "+"Type:"+super.getType()+" "+"Size:"+super.getSize()+" "+super.getPrice());
}
}
4.创建MyShoes类,实现Mycloth抽象类
/**
* @author Carl
* @version 1.0
* @date 2020/8/14 15:50
* @description
**/
public class MyShoes extends MyCloth {
@Override
public void getMsg() {
System.out.println("Name:"+super.getName()+" "+"Type:"+super.getType()+" "+"Size:"+super.getSize()+" "+super.getPrice());
}
}
5.创建提供对象复制功能的MyClothClone类
import java.util.HashMap;
/**
* @author Carl
* @version 1.0
* @date 2020/8/14 15:51
* @description
**/
public class MyClothClone {
private static HashMap<String,Object> myClothMap = new HashMap<>(16);
public static MyCloth getMyCloth(String name){
MyCloth myCloth = (MyCloth) myClothMap.get(name);
return (MyCloth) myCloth.clone();
}
public static void addMyCloth(){
MyShirt Tshirt = new MyShirt();
Tshirt.setName("T-shirt");
Tshirt.setType("shirt");
Tshirt.setSize("35");
Tshirt.setPrice(100);
MyPants WsPants = new MyPants();
WsPants.setName("Western-style pants");
WsPants.setType("pants");
WsPants.setSize("30");
WsPants.setPrice(200);
MyShoes shoes = new MyShoes();
shoes.setName("sport pants");
shoes.setType("shoes");
shoes.setSize("42.5");
shoes.setPrice(300);
myClothMap.put("Tshirt",Tshirt);
myClothMap.put("WsPants",WsPants);
myClothMap.put("shoes",shoes);
}
}
6.测试类
/**
* @author Carl
* @version 1.0
* @date 2020/8/14 15:46
* @description
**/
public class Domian {
public static void main(String[] args) {
MyClothClone.addMyCloth();
MyShirt myCloth = (MyShirt) MyClothClone.getMyCloth("Tshirt");
myCloth.getMsg();
MyPants myPants = (MyPants) MyClothClone.getMyCloth("WsPants");
myPants.getMsg();
MyShoes myShoes = (MyShoes) MyClothClone.getMyCloth("shoes");
myShoes.getMsg();
MyShoes myShoes1 = (MyShoes) MyClothClone.getMyCloth("shoes");
System.out.println(myShoes == myShoes1);
}
}
7.测试结果
Name:T-shirt Type:shirt Size:35 100.0
Name:Western-style pants Type:pants Size:30 200.0
Name:sport pants Type:shoes Size:42.5 300.0
复制的对象属性:
Name:sport pants Type:shoes Size:42.5 300.0
两个对象是否为同一个对象:false
说明:
原型模式最重要的步骤就是MyCloth抽象类实现cloneable类,并重写clone方法,调用父类方法,返回父类方法返回的对象。不过这种拷贝是深拷贝,用同一个可复制的对象连续复制两次,得到的两个对象的地址不同,在操作其中一个对象时,另一个不会随之改变。