博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JAVA设计模式之【原型模式】
阅读量:6608 次
发布时间:2019-06-24

本文共 6415 字,大约阅读时间需要 21 分钟。

422101-20161001080404360-1750947788.png

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?falseemail.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?falseemail.getAttachment==copyEmail.getAttachment?false

点评

通过流实现了深克隆,把对象中的值类型和引用类型都克隆了。
这种方式比较复杂一点,可以根据实际情况来选择使用。

本文转自TBHacker博客园博客,原文链接:http://www.cnblogs.com/jiqing9006/p/5925430.html,如需转载请自行联系原作者

你可能感兴趣的文章
找到一个适合的分布式文件系统之各种分布式文件系统优缺点对比
查看>>
索引失效的几个原因
查看>>
五险一金,你清楚吗?
查看>>
Ip核_fifo
查看>>
repquota命令--Linux命令应用大词典729个命令解读
查看>>
rabbitmq 管理及常用命令
查看>>
HTTP要被抛弃? 亚洲诚信携手宝塔开启HTTPS加密快速通道
查看>>
6.6 tar打包
查看>>
Spring MVC核心技术
查看>>
TCP协议如何保证传输的可靠性
查看>>
Spring Cloud云架构 - SSO单点登录之OAuth2.0 登出流程(3)
查看>>
软件开发各阶段交付物列表
查看>>
ntp服务器的搭建
查看>>
六、nginx搭建织梦DedeCms网站
查看>>
Tair学习小记
查看>>
网卡绑定(服务器&&交换机),缓存服务器Squid架构配置
查看>>
web网站加速之CDN(Content Delivery Network)技术原理
查看>>
sed的基本用法
查看>>
一个不错的shell 脚本入门教程
查看>>
Ansible之playbook的使用
查看>>