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

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



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

相关文章
|
2天前
|
数据采集 人工智能 安全
|
11天前
|
云安全 监控 安全
|
3天前
|
自然语言处理 API
万相 Wan2.6 全新升级发布!人人都能当导演的时代来了
通义万相2.6全新升级,支持文生图、图生视频、文生视频,打造电影级创作体验。智能分镜、角色扮演、音画同步,让创意一键成片,大众也能轻松制作高质量短视频。
1017 151
|
3天前
|
编解码 人工智能 机器人
通义万相2.6,模型使用指南
智能分镜 | 多镜头叙事 | 支持15秒视频生成 | 高品质声音生成 | 多人稳定对话
|
16天前
|
机器学习/深度学习 人工智能 自然语言处理
Z-Image:冲击体验上限的下一代图像生成模型
通义实验室推出全新文生图模型Z-Image,以6B参数实现“快、稳、轻、准”突破。Turbo版本仅需8步亚秒级生成,支持16GB显存设备,中英双语理解与文字渲染尤为出色,真实感和美学表现媲美国际顶尖模型,被誉为“最值得关注的开源生图模型之一”。
1711 9
|
8天前
|
人工智能 自然语言处理 API
一句话生成拓扑图!AI+Draw.io 封神开源组合,工具让你的效率爆炸
一句话生成拓扑图!next-ai-draw-io 结合 AI 与 Draw.io,通过自然语言秒出架构图,支持私有部署、免费大模型接口,彻底解放生产力,绘图效率直接爆炸。
654 152
|
10天前
|
人工智能 安全 前端开发
AgentScope Java v1.0 发布,让 Java 开发者轻松构建企业级 Agentic 应用
AgentScope 重磅发布 Java 版本,拥抱企业开发主流技术栈。
620 12
|
10天前
|
人工智能 自然语言处理 API
Next AI Draw.io:当AI遇见Draw.io图表绘制
Next AI Draw.io 是一款融合AI与图表绘制的开源工具,基于Next.js实现,支持自然语言生成架构图、流程图等专业图表。集成多款主流大模型,提供智能绘图、图像识别优化、版本管理等功能,部署简单,安全可控,助力技术文档与系统设计高效创作。
690 151