前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >设计模式~原始模型模式(二)

设计模式~原始模型模式(二)

作者头像
Vincent-yuan
发布2020-08-17 01:14:05
3270
发布2020-08-17 01:14:05
举报
文章被收录于专栏:Vincent-yuanVincent-yuan

这里给出一个原始模型模式的例子。孙大圣的身外身法术

浅复制

设计图如下:

TheGreatestSage类扮演客户角色。

TheGreatestSage类

package com.vincent.prototype;

public class TheGreatestSage {

    private Monkey monkey = new Monkey();
    public void change(){
        Monkey copyMonkey;
        for(int i=0;i<2000;i++){};
        copyMonkey = (Monkey)monkey.clone();
        System.out.println("Monkey King's birth date="+monkey.getBirthDate());
        System.out.println("Copy monkey's birth date="+copyMonkey.getBirthDate());
        System.out.println("Monkey King == Copy Monkey?"+(monkey==copyMonkey));
        System.out.println("Monkey King's Staff==Copy Monkey's Staff?"+(monkey.getStaff()==copyMonkey.getStaff()));
        
    }
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        TheGreatestSage sage = new TheGreatestSage();
        sage.change();
        
    }

}

Monkey类

package com.vincent.prototype;

import java.util.Date;

public class Monkey implements Cloneable {

    private int height;
    private int weight;
    private GoldRingedStaff staff;
    private Date birthDate;
    
    /**
     * 构造函数
     */
    public Monkey(){
        this.birthDate = new Date();
    }
    
    public Object clone(){
        Monkey temp = null;
        try{
            temp = (Monkey)super.clone();
        }catch(CloneNotSupportedException e){
            System.out.println("Clone failed");
        }finally{
            return temp;
        }
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    public Date getBirthDate() {
        return birthDate;
    }

    public void setBirthDate(Date birthDate) {
        this.birthDate = birthDate;
    }

    public GoldRingedStaff getStaff() {
        return staff;
    }

    public void setStaff(GoldRingedStaff staff) {
        this.staff = staff;
    }
    
}

GoldRingedStaff类

package com.vincent.prototype;

public class GoldRingedStaff {

    private float height = 100.F;
    private float diameter = 10.F;
    
    /**
     * 构造函数
     */
    public GoldRingedStaff(){
        // write your code here
    }
    /**
     * 增长行为
     */
    public void grow(){
        this.diameter *=2.0;
        this.height *=2.0;
    }
    /**
     * 缩小行为
     */
    public void shrink(){
        this.diameter /= 2;
        this.height /= 2;
    }
    
    public void move(){
        //wirte you code for moving the staff
    }
    public float getHeight() {
        return height;
    }
    public void setHeight(float height) {
        this.height = height;
    }
    public float getDiameter() {
        return diameter;
    }
    public void setDiameter(float diameter) {
        this.diameter = diameter;
    }
    
}

运行结果如下:

Monkey King's birth date=Fri Aug 14 07:15:59 CST 2020 Copy monkey's birth date=Fri Aug 14 07:15:59 CST 2020 Monkey King == Copy Monkey?false Monkey King's Staff==Copy Monkey's Staff?true

深复制

为做到深复制,所有需要复制的对象需要实现 java.io.Serializable接口。

系统设计如下:

TheGreatestSage类

package com.vincent.prototype;

import java.io.IOException;

public class TheGreatestSage {

    private Monkey monkey = new Monkey();
    public void change() throws IOException,ClassNotFoundException{
        Monkey copyMonkey;
        for(int i=0;i<2000;i++){};
        copyMonkey = (Monkey)monkey.deepClone();
        System.out.println("Monkey King's birth date="+monkey.getBirthDate());
        System.out.println("Copy monkey's birth date="+copyMonkey.getBirthDate());
        System.out.println("Monkey King == Copy Monkey?"+(monkey==copyMonkey));
        System.out.println("Monkey King's Staff==Copy Monkey's Staff?"+(monkey.getStaff()==copyMonkey.getStaff()));
        
    }
    
    public static void main(String[] args) throws IOException,ClassNotFoundException{
        // TODO Auto-generated method stub

        TheGreatestSage sage = new TheGreatestSage();
        sage.change();
        
    }

}

Monkey

package com.vincent.prototype;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OptionalDataException;
import java.io.Serializable;
import java.util.Date;

public class Monkey implements Cloneable,Serializable {

    private int height;
    private int weight;
    private GoldRingedStaff staff;
    private Date birthDate;
    
    /**
     * 构造函数
     */
    public Monkey(){
        this.birthDate = new Date();
        this.staff = new GoldRingedStaff();
    }
    /**
     * 深克隆方法
     * @return
     * @throws IOException
     * @throws OptionalDataException
     * @throws ClassNotFoundException
     */
    public Object deepClone() throws IOException,OptionalDataException,ClassNotFoundException
    {
        //首先将对象写出到流里
        ByteArrayOutputStream bo = new ByteArrayOutputStream();
        ObjectOutputStream oo = new ObjectOutputStream(bo);
        oo.writeObject(this);
        //然后将对象从流里读出来
        ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());
        ObjectInputStream oi = new ObjectInputStream(bi);
        
        return (oi.readObject());
    }
    /**
     * 浅克隆方法
     */
    public Object clone(){
        Monkey temp = null;
        try{
            temp = (Monkey)super.clone();
        }catch(CloneNotSupportedException e){
            System.out.println("Clone failed");
        }finally{
            return temp;
        }
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    public Date getBirthDate() {
        return birthDate;
    }

    public void setBirthDate(Date birthDate) {
        this.birthDate = birthDate;
    }

    public GoldRingedStaff getStaff() {
        return staff;
    }

    public void setStaff(GoldRingedStaff staff) {
        this.staff = staff;
    }
    
}

GoldRingedStaff

package com.vincent.prototype;

import java.io.Serializable;

public class GoldRingedStaff implements Cloneable,Serializable{

    private float height = 100.F;
    private float diameter = 10.F;
    
    /**
     * 构造函数
     */
    public GoldRingedStaff(){
        // write your code here
    }
    /**
     * 增长行为
     */
    public void grow(){
        this.diameter *=2.0;
        this.height *=2.0;
    }
    /**
     * 缩小行为
     */
    public void shrink(){
        this.diameter /= 2;
        this.height /= 2;
    }
    
    public void move(){
        //wirte you code for moving the staff
    }
    public float getHeight() {
        return height;
    }
    public void setHeight(float height) {
        this.height = height;
    }
    public float getDiameter() {
        return diameter;
    }
    public void setDiameter(float diameter) {
        this.diameter = diameter;
    }
    
}

结果如下:

Monkey King's birth date=Fri Aug 14 07:37:04 CST 2020 Copy monkey's birth date=Fri Aug 14 07:37:04 CST 2020 Monkey King == Copy Monkey?false Monkey King's Staff==Copy Monkey's Staff?false

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-08-14 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 浅复制
  • 深复制
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档