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
点评
通过流实现了深克隆,把对象中的值类型和引用类型都克隆了。
这种方式比较复杂一点,可以根据实际情况来选择使用。
JAVA设计模式之【原型模式】的更多相关文章
- JAVA 设计模式之原型模式
目录 JAVA 设计模式之原型模式 简介 Java实现 1.浅拷贝 2.深拷贝 优缺点说明 1.优点 2.缺点 JAVA 设计模式之原型模式 简介 原型模式是六种创建型设计模式之一,主要应用于创建相同 ...
- java设计模式4——原型模式
java设计模式4--原型模式 1.写在前面 本节内容与C++语言的复制构造函数.浅拷贝.深拷贝极为相似,因此建议学习者可以先了解C++的该部分的相关知识,或者学习完本节内容后,也去了解C++的相应内 ...
- java设计模式之原型模式
原型模式概念 该模式的思想就是将一个对象作为原型,对其进行复制.克隆,产生一个和原对象类似的新对象.java中复制通过clone()实现的.clone中涉及深.浅复制.深.浅复制的概念如下: ⑴浅复制 ...
- java设计模式之五原型模式(Prototype)
原型模式虽然是创建型的模式,但是与工程模式没有关系,从名字即可看出,该模式的思想就是将一个对象作为原型,对其进行复制.克隆,产生一个和原对象类似的新对象.本小结会通过对象的复制,进行讲解.在Java中 ...
- JAVA设计模式之 原型模式【Prototype Pattern】
一.概述: 使用原型实例指定创建对象的种类,而且通过拷贝这些原型创建新的对象. 简单的说就是对象的拷贝生成新的对象(对象的克隆),原型模式是一种对象创建型模式. 二.使用场景: 创建新的对象能够通过对 ...
- JAVA 设计模式之 原型模式详解
原型模式(Prototype Pattern)是指原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象. 原型模式利用的是克隆的原理,创建新的对象,JDK提供的Cloneable 和JSON. ...
- JAVA设计模式之原型模式(prototype)
原型模式: 原型模式又叫克隆模式 Java自带克隆模式 实现克隆模式必须实现Cloneable 接口,如果不实现会发生java.lang.CloneNotSupportedException异常 当某 ...
- 孙悟空的身外身法术使用了Java设计模式:原型模式
目录 定义 意图 主要解决问题 何时使用 优缺点 结构 简单形式的原型模式 登记形式的原型模式 两种形式比较 浅克隆和深克隆 孙悟空的身外身法术 浅克隆实现 深克隆实现 定义 原型模式属于对象的创建型 ...
- 设计模式_11_原型模式(prototype)深拷贝、浅拷贝
设计模式_11_原型模式(prototype) 浅拷贝: package designPatternOf23; /** * 定义:用原型实例,指定创建对象的种类,并通过拷贝这些原型创建新的对象 * P ...
- 【GOF23设计模式】原型模式
来源:http://www.bjsxt.com/ 一.[GOF23设计模式]_原型模式.prototype.浅复制.深复制.Cloneable接口 浅复制 package com.test.prot ...
随机推荐
- js与jquery基础知识对比(一)---2017-05-06
用表格做的,想要对比的内容一目了然,红色部分为重点 js jquery 取元素 id: document.getElementById("aa"); 取到的是dom对象 cla ...
- Scrapy日志等级以及请求传参
日志等级 请求传参 提高scrapy的爬取效率 日志等级 - 日志信息: 使用命令:scrapy crawl 爬虫文件 运行程序时,在终端输出的就是日志信息: - 日志信息的种类: - ERROR ...
- jbox如果弹不出,放在body里
body> <form id="form1" runat="server"> <script type="text/javas ...
- 判断输入的值是否为Double
using System; using System.Collections.Generic; using System.Text; namespace TDRFactory { public cla ...
- 用opcity模拟zindex渐变的效果
github地址: https://github.com/echoorx/opacity-Gradient zindex好像不能渐变改变,所以用opcity来模拟 <!DOCTYPE html& ...
- css relative设置top为百分比值
前言: 最近在学习HTML.CSS的过程中,想模仿一下百度首页.发现搜索框这一部分与上下其它元素的空白距离可以随着窗口大小变化(效果如下图所示),于是自己研究了一下并记录下来. 效果实现 <!D ...
- WPF开发“Program '*' does not contain a static 'Main' method suitable for an entry point”错误
WPF项目编译时出现“Program '*' does not contain a static 'Main' method suitable for an entry point”错误, 解决方 ...
- hdu3488 / hdu3435 / hdu1853 最小费用最大流 圈 拆点
题目大意: 在一个有向图中,求经过所有点的最小圈. 思路: (如果是用二分图的完美匹配来做,那么直接上模版就好了).http://www.cnblogs.com/Potato-lover/p/3991 ...
- hdu4009 Transfer water 最小树形图
每一户人家水的来源有两种打井和从别家接水,每户人家都可能向外输送水. 打井和接水两种的付出代价都接边.设一个超级源点,每家每户打井的代价就是从该点(0)到该户人家(1~n)的边的权值.接水有两种可能, ...
- vue-留言板-bootstrap
最近看完入门API,看完视频自己写了个留言板,因为主要是学习vue,所以就复习了一下bootstrap,布局更简单,先看看样式吧. 简单清晰的布局,先说一下功能, 1.输入用户名密码点击提交放入表格 ...