zoukankan      html  css  js  c++  java
  • 设计模式~原始模型模式(二)

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

    浅复制

    设计图如下:

     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

  • 相关阅读:
    重新想象 Windows 8 Store Apps (15) 控件 UI: 字体继承, Style, ControlTemplate, SystemResource, VisualState, VisualStateManager
    重新想象 Windows 8 Store Apps (12) 控件之 GridView 特性: 拖动项, 项尺寸可变, 分组显示
    返璞归真 asp.net mvc (10) asp.net mvc 4.0 新特性之 Web API
    与众不同 windows phone (29) Communication(通信)之与 OData 服务通信
    与众不同 windows phone (33) Communication(通信)之源特定组播 SSM(Source Specific Multicast)
    与众不同 windows phone (27) Feature(特性)之搜索的可扩展性, 程序的生命周期和页面的生命周期, 页面导航, 系统状态栏
    与众不同 windows phone (30) Communication(通信)之基于 Socket TCP 开发一个多人聊天室
    返璞归真 asp.net mvc (12) asp.net mvc 4.0 新特性之移动特性
    重新想象 Windows 8 Store Apps (2) 控件之按钮控件: Button, HyperlinkButton, RepeatButton, ToggleButton, RadioButton, CheckBox, ToggleSwitch
    重新想象 Windows 8 Store Apps (10) 控件之 ScrollViewer 特性: Chaining, Rail, Inertia, Snap, Zoom
  • 原文地址:https://www.cnblogs.com/Vincent-yuan/p/13500078.html
Copyright © 2011-2022 走看看