设计模式:15-原型模式

一、适用场景:通过已经存在的对象直接克隆出新对象的场景。比如有一个Person对象,其各个属性都已经设置完成,现在需要克隆出一个新的对象并进行少量修改。
二、示例
1.姓名类(Name)定义

package cn.edu.tju.app;

public class Name implements Cloneable{
    private String firstName;
    private String lastName;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @Override
    public Name clone() throws CloneNotSupportedException {
      return (Name)super.clone();
    }
}

2.人员(Person)类定义:

package cn.edu.tju.app;

public class Person implements Cloneable{
    private int id;
    private Name name;
    private int age;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Name getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public Person clone() throws CloneNotSupportedException {
        return (Person)super.clone();
    }
}

该类实现了Clonable接口,所以可以调用其clone方法进行克隆。
3.主类:

package cn.edu.tju.app;

public class PrototypeTest {
    public static void main(String[] args) throws Exception{
        Person p1=new Person();
        p1.setId(1);
        p1.setName(new Name());
        p1.getName().setFirstName("Albert");
        p1.getName().setLastName("Einstein");
        p1.setAge(222);

        Person p2=p1.clone();

        System.out.println(p1.getName()==p2.getName());
    }
}

程序的输出为true. 因此,可以看出p1中的name对象属性和p2中的name对象属性指向了同一个Name类对象,因为java语言本身提供的clone()方法只是简单的内存拷贝(浅拷贝)。要想使p1和p2中的两个name为不同的对象,除了调用super.clone()方法,还需要调用Name类中的clone()方法来进行name对象的拷贝,示例如下:

package cn.edu.tju.app;

public class People implements Cloneable {
    private int id;
    private Name name;
    private int age;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Name getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public People clone() throws CloneNotSupportedException {
        People people=(People)super.clone();
        Name newName=(Name)name.clone();
        people.setName(newName);
        return people;
    }
}

测试类如下:

package cn.edu.tju.app;

public class PrototypeTest2 {
    public static void main(String[] args) throws CloneNotSupportedException {
        People p1=new People();
        p1.setId(1);
        p1.setAge(222);
        p1.setName(new Name());
        p1.getName().setFirstName("Albert");
        p1.getName().setLastName("Einstein");

        People p2=p1.clone();
        System.out.println(p1.getName()==p2.getName());
    }
}

程序输出为false

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值