zoukankan      html  css  js  c++  java
  • JAVA设计模式之【原型模式】

    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
    

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

  • 相关阅读:
    第四篇 -- 收获颇丰的一天
    第十六篇 -- SuperIO学习
    第八篇 -- 用U盘制作启动盘装Win10系统
    第二十五篇 -- 学习第三十四天打卡20190726
    zabbix钉钉报警
    windows 远程连接“发生身份验证错误 要求的函数不受支持”
    限制IP远程访问
    查看、踢出在线用户
    codis
    Linux、windows安装java
  • 原文地址:https://www.cnblogs.com/jiqing9006/p/5925430.html
Copyright © 2011-2022 走看看