架构学习之路(一)源码分析 4 原型模式

11 篇文章 0 订阅

架构学习之路(一)源码分析 3 单例模式

  1. 原型模式(Prototype)

    1. 创建型
    2. 有一个原型
    3. 数据内容相同,但对象实例不同
    4. DTO、VO、POJO、Entity…
    5. DTO 和 VO 之间存在一些属性名称、类型都相同,数据库中表查询出来的对象会赋值给 DTO,MVC 中的 Model 把 DTO 中的值赋值给 VO,再把 VO 中的值传输到 View 中去
    6. 复制,属性名称相同、属性内容相同、属性值相同
    7. Spring scope=“prototype”:把对象中配置的依赖关系,在每次使用对象之前,都会创建一个新的对象,并且会将依赖关系完整的赋值给新创建的对象
    8. 实现方式
      1. 反射去实现,性能低,apache
      2. clone 实现,性能高
    9. 生活场景:孙悟空吹毫毛变猴子
    /**
     * 浅复制
     **/
    public class Prototype implements Cloneable{
        private String name;
    	private final ArrayList<String> arrayList = new ArrayList<String>();
    	
    	@Override
    	public Object clone() throws CloneNotSupportedException {
    		return super.clone();
    	}
    	
    	public String getName() {
    		return name;
    	}
    	
    	public void setName(String name) {
    		this.name = name;
    	}
    	
    	public ArrayList<String> getArrayList() {
    		return arrayList;
    	}
    	
    	public void add(String str){
    		arrayList.add(str);
    	}
    }
    
    public class Monkey {
    	protected double height;
    	protected double weight;
    	protected Date birthday;
    	
    	public double getHeight() {
    		return height;
    	}
    	
    	public void setHeight(double height) {
    		this.height = height;
    	}
    	
    	public double getWeight() {
    		return weight;
    	}
    	
    	public void setWeight(double weight) {
    		this.weight = weight;
    	}
    	
    	public Date getBirthday() {
    		return birthday;
    	}
    	
    	public void setBirthday(Date birthday) {
    		this.birthday = birthday;
    	}
    }
    
    public class JinGuBang implements Serializable {
    	private double height = 100;
    	
    	private double diameter = 10;
    	
    	public void big() {
    		this.diameter *= 2;
    		this.height *= 2;
    	}
    	
    	public void small() {
    		this.diameter /= 2;
    		this.height /= 2;
    	}
    }
    
    /**
     * 深复制
     */
    public class WuKong extends Monkey implements Cloneable,Serializable {
    	private final JinGuBang jinGuBang = new JinGuBang();
    	public WuKong(){
    		this.birthday = new Date();
    	}
    	
    	public JinGuBang getJinGuBang() {
    		return jinGuBang;
    	}
    	
    	@Override
    	public Object clone() throws CloneNotSupportedException {
    		super.clone();
    		return this.deepClone();
    	}
    	
    	private Object deepClone(){
    		try {
    			final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    			final ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
    			objectOutputStream.writeObject(this);
    			final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
    			final ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
    			return objectInputStream.readObject();
    		} catch (IOException | ClassNotFoundException e) {
    			e.printStackTrace();
    			return null;
    		}
    	}
    }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值