• 7、Prototype 原型模式 通过复制创造实例 创造型模式


       
    发哥讲

    ,clone,Prototype. ()

    1Prototype

    Prototype

    Java

    1implement Cloneableclone
    2Objectclone

    java8String

    2

    : ,,.

    Sheep

    package cn.design.prototype;

    import java.util.Date;

    /**
    * @author lin
    * @version 1.0
    * @date 2020/7/19 21:09
    * @Description 
    */
    public class Sheep implements Cloneable {
       String name;
       Date birthDay;

       public Sheep(String name, Date birthDay) {
           this.name = name;
           this.birthDay = birthDay;
      }

       @Override
       protected Object clone() throws CloneNotSupportedException {
           return super.clone();
      }


       public String getName() {
           return name;
      }

       public void setName(String name) {
           this.name = name;
      }

       public Date getBirthDay() {
           return birthDay;
      }

       public void setBirthDay(Date birthDay) {
           this.birthDay = birthDay;
      }
    }

    TestClone1

    package cn.design.prototype;

    import java.util.Date;

    /**
    * @author lin
    * @version 1.0
    * @date 2020/7/19 21:11
    * @Description  
    */
    public class TestClone1 {
       public static void main(String[] args) throws CloneNotSupportedException {
           Date date = new Date(21312312312L);
           Sheep s1 = new Sheep("", date);
           System.out.println("s1 = " + s1);
           System.out.println("s1.getName() = " + s1.getName());
           System.out.println("s1.getBirthDay() = " + s1.getBirthDay());

           System.out.println();

           Sheep s2 = (Sheep) s1.clone();
           System.out.println("s2 = " + s2);
           System.out.println("s2.getName() = " + s2.getName());
           System.out.println("s2.getBirthDay() = " + s2.getBirthDay());

           System.out.println();

           //  ,  
           s2.setName("");
           System.out.println("s2.getName() = " + s2.getName());
           // s1
           System.out.println("s1.getName() = " + s1.getName());

           System.out.println();

           //  ,    date,  date,  date
           date.setTime(435345353453L);
           s2.setBirthDay(date);

           System.out.println("s2.getBirthDay() = " + s2.getBirthDay());
           // s1
           System.out.println("s1.getBirthDay() = " + s1.getBirthDay());

      }
    }

    :

    s1 = cn.design.prototype.Sheep@135fbaa4
    s1.getName() = 
    s1.getBirthDay() = Sat Sep 05 00:05:12 CST 1970

    s2 = cn.design.prototype.Sheep@7ea987ac
    s2.getName() = 
    s2.getBirthDay() = Sat Sep 05 00:05:12 CST 1970

    s2.getName() = 
    s1.getName() = 

    s2.getBirthDay() = Wed Oct 19 01:15:53 CST 1983
    s1.getBirthDay() = Wed Oct 19 01:15:53 CST 1983

    3

    : ,.

    Sheep2

    package cn.design.prototype;

    import java.util.Date;

    /**
    * @author lin
    * @version 1.0
    * @date 2020/7/19 21:09
    * @Description 2
    */
    public class Sheep2 implements Cloneable {
       String name;
       Date birthDay;

       public Sheep2(String name, Date birthDay) {
           this.name = name;
           this.birthDay = birthDay;
      }

       @Override
       protected Object clone() throws CloneNotSupportedException {
           Date newDate = null;
           Sheep2 o = (Sheep2) super.clone();
           newDate = (Date) o.birthDay.clone();
           o.setBirthDay(newDate);
           return o;
      }


       public String getName() {
           return name;
      }

       public void setName(String name) {
           this.name = name;
      }

       public Date getBirthDay() {
           return birthDay;
      }

       public void setBirthDay(Date birthDay) {
           this.birthDay = birthDay;
      }
    }

    TestClone2

    package cn.design.prototype;

    import java.util.Date;

    /**
    * @author lin
    * @version 1.0
    * @date 2020/7/19 21:11
    * @Description  
    */
    public class TestClone2 {
       public static void main(String[] args) throws CloneNotSupportedException {
           Date date = new Date(21312312312L);
           Sheep2 s1 = new Sheep2("", date);
           System.out.println("s1 = " + s1);
           System.out.println("s1.getName() = " + s1.getName());
           System.out.println("s1.getBirthDay() = " + s1.getBirthDay());

           System.out.println();

           Sheep2 s2 = (Sheep2) s1.clone();
           System.out.println("s2 = " + s2);
           System.out.println("s2.getName() = " + s2.getName());
           System.out.println("s2.getBirthDay() = " + s2.getBirthDay());

           System.out.println();

           //  ,  
           s2.setName("");
           System.out.println("s2.getName() = " + s2.getName());
           // s1
           System.out.println("s1.getName() = " + s1.getName());

           System.out.println();

           // ,  
           date.setTime(435345353453L);
           s1.setBirthDay(date);

           System.out.println("s2.getBirthDay() = " + s2.getBirthDay());
           // s1
           System.out.println("s1.getBirthDay() = " + s1.getBirthDay());




      }
    }

    :

    s1 = cn.design.prototype.Sheep2@4ac68d3e
    s1.getName() = 
    s1.getBirthDay() = Sat Sep 05 00:05:12 CST 1970

    Sat Sep 05 00:05:12 CST 1970
    Sat Sep 05 00:05:12 CST 1970
    s2 = cn.design.prototype.Sheep2@27082746
    s2.getName() = 
    s2.getBirthDay() = Sat Sep 05 00:05:12 CST 1970

    s2.getName() = 
    s1.getName() = 

    s2.getBirthDay() = Sat Sep 05 00:05:12 CST 1970
    s1.getBirthDay() = Wed Oct 19 01:15:53 CST 1983
    VM, : ''127.0.0.1:14378', transport: ''', : '{1}'

    Process finished with exit code 0

    4?

    1

    2:

    new访使

    3

    访使使

    cloneJava使

    5

    1Prototype使(),.

    2Prototype"""",使"".,Clone.

    3PrototypeCloneObjectMemberwiseClone,:,,,使


                    

    圈~


    ● 扫码关注公众号, 转载请备注来源,和链接

  • 相关阅读:
    bzoj 2038
    ACM训练联盟周赛 A. Teemo's bad day
    The 2018 ACM-ICPC China JiangSu Provincial Programming Contest J. Set
    惊艳,Dubbo域名已改,也不再局限于Java!
    6月份值得一看的 Java 技术干货!
    90 % Java 程序员被误导的一个性能优化策略
    Spring Cloud Finchley 正式发布,包含 4 个重大更新!
    Java 11 快要来了,编译 & 运行一个命令搞定!
    Spring Boot 单元测试详解+实战教程
    Java 10 实战第 1 篇:局部变量类型推断
  • 原文地址:https://www.cnblogs.com/naimao/p/13353477.html
Copyright © 2020-2023  润新知