• java: Prototype Pattern


    /**
     * 版权所有 2022 涂聚文有限公司
     * 许可信息查看:
     * 描述:
     * 原始模型 Prototype Patterns
     * 历史版本: JDK 14.02
     * 2022-09-12 创建者 geovindu
     * 2022-09-12 添加 Lambda
     * 2022-09-12 修改:date
     * 接口类
     * 2022-09-12 修改者:Geovin Du
     * 生成API帮助文档的指令:
     *javadoc - -encoding Utf-8 -d apidoc PandaToClone.java
     *
     * */
    
    
    
    package com.javapatterns.prototype;
    
    import java.lang.Object.*;
    
    public class PandaToClone implements Cloneable {
    
        private int height, weight, age;
    
        public PandaToClone (int height, int weight)
        {
            this.age = 0;
            this.weight = weight;
            this.height = height;
        }
    
        public void setAge(int age)
        {
            this.age = age;
        }
        public int getAge()
        {
            return age;
        }
        public int getHeight()
        {
            return height;
        }
        public int getWeight()
        {
            return weight;
        }
    
        public Object clone()
        {
            PandaToClone temp = new PandaToClone(height, weight);
            temp.setAge(age);
    
            return (Object) temp;
        }
    
    }
    

      

    调用测试:

    //原始模型
                thisPanda = new PandaToClone(15, 25);
                thisPanda.setAge(3);
    
                // Create the second object by cloning the first
                thatPanda = (PandaToClone) thisPanda.clone();
    
                // Now describe these objects on the system console :
    
                System.out.println(" Age of this panda : " + thisPanda.getAge());
                System.out.println("            height : " + thisPanda.getHeight());
                System.out.println("            weight : " + thisPanda.getWeight());
                System.out.println(" Age of that panda : " + thatPanda.getAge());
                System.out.println("            height : " + thatPanda.getHeight());
                System.out.println("            weight : " + thatPanda.getWeight());
    

      

    输出:

     Age of this panda : 3
                height : 15
                weight : 25
     Age of that panda : 3
                height : 15
                weight : 25
    

      

    /**
     * 版权所有 2022 涂聚文有限公司
     * 许可信息查看:
     * 描述:
     * 原始模型 Prototype Patterns
     * 历史版本: JDK 14.02
     * 2022-09-12 创建者 geovindu
     * 2022-09-12 添加 Lambda
     * 2022-09-12 修改:date
     * 接口类
     * 2022-09-12 修改者:Geovin Du
     * 生成API帮助文档的指令:
     *javadoc - -encoding Utf-8 -d apidoc Monkey.java
     *
     * */
    
    
    
    package com.javapatterns.prototype;
    
    
    import java.util.Date;
    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.ObjectOutputStream;
    import java.io.ObjectInputStream;
    import java.io.IOException;
    import java.io.OptionalDataException;
    import java.io.Serializable;
    
    /**
     * 大圣,猢狲
     *
     * */
    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();
        }
    
        /**
         *深克隆方法
         *
         * */
        public Object deepClone()
                throws IOException, OptionalDataException, ClassNotFoundException
        {
            //write to stream
            ByteArrayOutputStream bo = new ByteArrayOutputStream();
            ObjectOutputStream oo = new ObjectOutputStream(bo);
            oo.writeObject(this);
    
            //read from stream
            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;
        }
    
    
    
    
    }
    

      

    /**
     * 版权所有 2022 涂聚文有限公司
     * 许可信息查看:
     * 描述:
     * 原始模型 Prototype Patterns
     * 历史版本: JDK 14.02
     * 2022-09-12 创建者 geovindu
     * 2022-09-12 添加 Lambda
     * 2022-09-12 修改:date
     * 接口类
     * 2022-09-12 修改者:Geovin Du
     * 生成API帮助文档的指令:
     *javadoc - -encoding Utf-8 -d apidoc GoldRingedStaff.java
     *
     * */
    
    
    package com.javapatterns.prototype;
    
    import java.util.Date;
    import java.io.Serializable;
    
    /**
     * 金箍棒
     *
     * */
    public class GoldRingedStaff  implements Cloneable, Serializable{
    
        private float height = 100.0F;
        private float diameter = 10.0F;
        /**
         *
         * */
        public GoldRingedStaff()
        {
            //write your code here
        }
        /**
         *
         * */
        public void grow()
        {
            this.diameter *= 2.0;
            this.height *= 2;
        }
        /**
         *
         * */
        public void shrink()
        {
            this.diameter /= 2;
            this.height /= 2;
        }
        /**
         *
         * */
        public void move()
        {
            //write your 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;
        }
    
    
    
    }
    

      

    /**
     * 版权所有 2022 涂聚文有限公司
     * 许可信息查看:
     * 描述:
     * 原始模型 Prototype Patterns
     * 历史版本: JDK 14.02
     * 2022-09-12 创建者 geovindu
     * 2022-09-12 添加 Lambda
     * 2022-09-12 修改:date
     * 接口类
     * 2022-09-12 修改者:Geovin Du
     * 生成API帮助文档的指令:
     *javadoc - -encoding Utf-8 -d apidoc TheGreatestSage.java
     *
     * */
    
    
    package com.javapatterns.prototype;
    
    
    import java.util.Date;
    import java.io.IOException;
    import java.lang.ClassNotFoundException;
    
    /**
     * 大圣
     * */
    public class TheGreatestSage {
    
    
        private Monkey monkey = new Monkey();
    
        public TheGreatestSage()
        { }
    
        /**
         *
         *
         * */
        public void change() throws IOException, ClassNotFoundException
        {
    
            try {
                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()));
            }
            catch (ClassNotFoundException cex)
            {
                cex.printStackTrace();
            }
            catch(IOException ioException)
            {
                ioException.printStackTrace();
            }
        }
    
    }
    

      

    调用测试:

     try
            {
    
                TheGreatestSage theGreatestSage=new TheGreatestSage();
                theGreatestSage.change();
            }
            catch (Exception exception)
            {
                exception.printStackTrace();
            }
    

      

    输出:

    Monkey King's birth date=Fri Sep 16 22:08:46 CST 2022
    Copy monkey's birth date=Fri Sep 16 22:08:46 CST 2022
    Monkey King == Copy Monkey? false
    Monkey King's Staff == Copy Monkey's Staff? false
    

      

  • 相关阅读:
    【FPGA】结构化层次化设计
    【FPGA】库、包和子程序 过程 函数
    【FPGA】8位奇偶校验 / 加(减)法器 VHDL程序
    【FPGA】顺序语句
    【FPGA】并行语句
    【FPGA】VHDL基础
    Luogu P3349 小星星
    Luogu P4284 概率充电器
    NOIP2018 | D2T2 & D2T3
    二项式反演 小记
  • 原文地址:https://www.cnblogs.com/geovindu/p/16701194.html
Copyright © 2020-2023  润新知