java设计模式——Prototype(原型模式)

 原型模式可以通过一个对象实例确定创建对象的种类,并且通过拷贝创建新的实例。总得来说,原型模式实际上就是从一个对象创建另一个新的对象,使新的对象有具有原对象的特征。

 

1.案例一
学生复制

package Prototype;

/**
 * Created by Jim on 2016/10/1.
 */
public class Student implements Cloneable{
    private String stuName;
    private String stuSex;
    private int stuAge;
    private String stuMajor;
    private String stuCollege;
    private String stuUniversity;

    public Student(String stuName,String stuSex,int stuAge,String stuMajor,String stuCollege,String stuUniversity) { // 构造函数
        this.stuName = stuName;
        this.stuSex = stuSex;
        this.stuAge = stuAge;
        this.stuMajor = stuMajor;
        this.stuCollege = stuCollege;
        this.stuUniversity = stuUniversity;
    }

    public void setStuName(String stuName) {
        this.stuName = stuName;
    }

    public void setStuSex(String stuSex) {
        this.stuSex = stuSex;
    }

    public void setStuAge(int stuAge) {
        this.stuAge = stuAge;
    }

    public void setStuMajor(String stuMajor) {
        this.stuMajor = stuMajor;
    }

    public void setStuCollege(String stuCollege) {
        this.stuCollege = stuCollege;
    }

    public void setStuUniversity(String stuUniversity) {
        this.stuUniversity = stuUniversity;
    }

    public String getStuName() {
        return this.stuName;
    }

    public String getStuSex() {
        return this.stuSex;
    }

    public int getStuAge() {
        return this.stuAge;
    }

    public String getStuMajor() {
        return this.stuMajor;
    }

    public String getStuCollege() {
        return this.stuCollege;
    }

    public String getStuUniversity() {
        return this.stuUniversity;
    }

    public Student clone() { // 实现克隆
        Student cpStudent = null;
        try{
            cpStudent=(Student)super.clone();
        }catch (CloneNotSupportedException e) {

        }
        return cpStudent;
    }
}

// 继续写类
class MainClass
{
    public static void main(String args[]) {
        Student stu1,stu2,stu3;
        stu1 = new Student("张无忌","男",24,"软件","信息工程学院","南京财经大学");

        // 使用原型
        stu2 = stu1.clone();
        stu2.setStuName("杨过");

        stu3 = stu1.clone();
        stu3.setStuName("小龙女");
        stu3.setStuSex("女");

        System.out.print("姓名:" + stu1.getStuName());
        System.out.print(",性别:" + stu1.getStuSex());
        System.out.print(",年龄:" + stu1.getStuAge());
        System.out.print(",专业:" + stu1.getStuMajor());
        System.out.print(",学院:" + stu1.getStuCollege());
        System.out.print(",大学:" + stu1.getStuUniversity());
        System.out.println();


        System.out.print("姓名:" + stu2.getStuName());
        System.out.print(",性别:" + stu2.getStuSex());
        System.out.print(",年龄:" + stu2.getStuAge());
        System.out.print(",专业:" + stu2.getStuMajor());
        System.out.print(",学院:" + stu2.getStuCollege());
        System.out.print(",大学:" + stu2.getStuUniversity());
        System.out.println();

        System.out.print("姓名:" + stu3.getStuName());
        System.out.print(",性别:" + stu3.getStuSex());
        System.out.print(",年龄:" + stu3.getStuAge());
        System.out.print(",专业:" + stu3.getStuMajor());
        System.out.print(",学院:" + stu3.getStuCollege());
        System.out.print(",大学:" + stu3.getStuUniversity());
        System.out.println();
    }
}

执行结果:
姓名:张无忌,性别:男,年龄:24,专业:软件,学院:信息工程学院,大学:南京财经大学
姓名:杨过,性别:男,年龄:24,专业:软件,学院:信息工程学院,大学:南京财经大学
姓名:小龙女,性别:女,年龄:24,专业:软件,学院:信息工程学院,大学:南京财经大学

2.邮件与附件
附件类

package Prototype;

/**
 * Created by Jim on 2016/10/1.
 */
public class Attachment {
    public void download() {
        System.out.println("下载附件");
    }
}

邮件类

package Prototype;

/**
 * Created by e550 on 2016/10/1.
 */
public class Email implements Cloneable{
    private Attachment attachment = null;

    public Email() {
        this.attachment = new Attachment();
    }

    public Object clone() {
        Email clone = null;
        try{
            clone=(Email)super.clone();
        }catch (CloneNotSupportedException e){
            System.out.println("Clone failure!");
        }
        return clone;
    }

    public Attachment getAttachment() {
        return this.attachment;
    }

    public void display() {
        System.out.println("查看邮件");
    }
}

客户端

package Prototype;

/**
 * Created by e550 on 2016/10/1.
 */
public class Client {
    public static void main(String a[]) {
        Email email,cpEmail;
        email = new Email();
        cpEmail = (Email)email.clone();

        System.out.println("email==cpEmail?");
        System.out.println(email==cpEmail);

        System.out.println("email.getAttachment==cpEmail.getAttachment?");
        System.out.println(email.getAttachment()==cpEmail.getAttachment());
    }
}

执行结果

email==cpEmail?
false
email.getAttachment==cpEmail.getAttachment?
true

说明:
这种克隆,没有把引用的变量克隆出来。

3.改造邮件类,通过流实现深克隆
附件类

package Prototype.deepClone;
import java.io.*;

/**
 * Created by e550 on 2016/10/1.
 */
public class Attachment implements Serializable{
    public void download() {
        System.out.println("下载附件");
    }
}

邮件类

package Prototype.deepClone;
import java.io.*;
/**
 * Created by e550 on 2016/10/1.
 */
public class Email implements Serializable{
    private Attachment attachment = null;
    public Email() {
        this.attachment = new Attachment();
    }

    public Object deepClone() throws IOException , ClassNotFoundException, OptionalDataException
    {
        // 将对象写入流中
        ByteArrayOutputStream bao = new ByteArrayOutputStream();
        ObjectOutputStream oss = new ObjectOutputStream(bao);
        oss.writeObject(this);

        //将对象从流中取出
        ByteArrayInputStream bis = new ByteArrayInputStream(bao.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bis);
        return(ois.readObject());
    }

    public Attachment getAttachment() {
        return this.attachment;
    }

    public void display() {
        System.out.println("查看邮件");
    }

}

客户端类

package Prototype.deepClone;

public class Client
{
    public static void main(String a[])
    {
        Email email,copyEmail=null;

        email=new Email();

        try{
            copyEmail=(Email)email.deepClone();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }


        System.out.println("email==copyEmail?");
        System.out.println(email==copyEmail);

        System.out.println("email.getAttachment==copyEmail.getAttachment?");
        System.out.println(email.getAttachment()==copyEmail.getAttachment());
    }
}

执行结果

email==copyEmail?
false
email.getAttachment==copyEmail.getAttachment?
false

转载于:https://my.oschina.net/u/3345581/blog/891092

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值