设计模式(四)原型模式(创建型)

本文详细介绍了Java中的原型模式,包括其实现方式、类图及浅拷贝与深拷贝的区别。并通过具体示例展示了如何使用原型模式进行对象的克隆。

定义:通过拷贝一个已经存在的实例来返回新的实例,而不是新建实例。被拷贝的实例就称为原型。

类图

原型类实现思路

 (1)实现Cloneable接口。(在Java虚拟机中,只有实现了这个接口的类才可以被拷贝。)

 (2)重写Object类中的clone方法。(作用是返回对象的一个拷贝,但其作用域是protected,要修改成public。)

原型模式中的拷贝分为“浅拷贝”和“深拷贝”。

浅拷贝

   对值类型的成员变量进行值的复制,对引用类型的成员变量只复制引用、不复制引用的对象。

深拷贝

   对值类型的成员变量进行值的复制,对引用类型的成员变量也进行引用对象的复制。

   Object类的clone方法只会拷贝对象中的基本的数据类型,对于String类型、数组、容器对象、引用对象等都不会拷贝。如果要实现深拷贝,必须对String类型、数组、容器对象、引用对象等另行拷贝。Java提供的大部分容器类都实现了Cloneable接口。

例子

   原型类

public class Prototype implements Cloneable{
    
    private String name;
    
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Prototype clone(){
        Prototype prototype = null;
        try{  
            prototype = (Prototype)super.clone();  
        }catch(CloneNotSupportedException e){  
            e.printStackTrace();  
        }  
        return prototype;
    }

}

     浅拷贝实现类

public class LightPrototype extends Prototype{
    
    public void display(){
        System.out.println(this.getName());
    }

}

   测试类

public class LightPrototypeTest {

    public static void main(String[] args){
        LightPrototype lightPrototype = new LightPrototype();
        lightPrototype.setName("original lightPrototype!");
        
        LightPrototype copyLightPrototypePrototype = (LightPrototype)lightPrototype.clone();
        
        System.out.println(copyLightPrototypePrototype.getName());
        
    }
    
}

   深拷贝实现类

public class DeepPrototype implements Cloneable{
    
    private String id;
    
    private Prototype prototype;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
    
    public Prototype getPrototype() {
        return prototype;
    }

    public void setPrototype(Prototype prototype) {
        this.prototype = prototype;
    }

    @Override
    public DeepPrototype clone(){
        DeepPrototype deepPrototype = null;
        try{  
            deepPrototype = (DeepPrototype)super.clone();  
            //对引用对象的拷贝
            deepPrototype.prototype = (Prototype)this.prototype.clone();
        }catch(CloneNotSupportedException e){  
            e.printStackTrace();  
        }  
        return deepPrototype;
    }

}

    测试类

public class DeepPrototypeTest {

    public static void main(String[] args){
        Prototype prototype =  new Prototype();
        prototype.setName("original Prototype!");
        DeepPrototype deepPrototype = new DeepPrototype();
        deepPrototype.setId("deepPrototype id!");
        deepPrototype.setPrototype(prototype);
        
        DeepPrototype copyDeepPrototypePrototype = (DeepPrototype)deepPrototype.clone();
        
        System.out.println(copyDeepPrototypePrototype.getId());
        System.out.println(copyDeepPrototypePrototype.getPrototype().getName());
        
    }
    
}

 



转载于:https://www.cnblogs.com/xujianfang/p/5923413.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值