• 设计模式原型模式


    -------------------------------浅拷贝-----------------------------------------
    @Data
    public class ShopItem implements Cloneable {
    private String shopItemName;
    public ShopItem clone() throws CloneNotSupportedException {
    ShopItem clone = (ShopItem) super.clone();
    return clone;
    }
    }

    @Data
    public class Shop implements Cloneable {
        private String shopName;
    private Integer shopNumber;
    private ShopItem shopItem;

    public Shop(String shopName, Integer shopNumber, ShopItem shopItem) {
    this.shopName = shopName;
    this.shopNumber = shopNumber;
    this.shopItem = shopItem;
    }

    public static void main(String[] args) throws CloneNotSupportedException {
    ShopItem shopItem = new ShopItem();
    shopItem.setShopItemName("shopItemName1");
    Shop shop = new Shop("shopName1", 10, shopItem);
    Shop clone = shop.clone();
    System.out.println("shop: " + shop);
    System.out.println("clone: " + clone);
    System.out.println("-----------------");
    shopItem.setShopItemName("shopItemName2");
    System.out.println("shop: " + shop);
    System.out.println("clone: " + clone);
    }

    public Shop clone() throws CloneNotSupportedException {
    Shop shop = (Shop) super.clone();
    // ShopItem clone = this.shopItem.clone();
    // shop.setShopItem(clone);
    return shop;
    }
    }

    打印结果:

    shop: Shop(shopName=shopName1, shopNumber=10, shopItem=ShopItem(shopItemName=shopItemName1))
    clone: Shop(shopName=shopName1, shopNumber=10, shopItem=ShopItem(shopItemName=shopItemName1))
    -----------------
    shop: Shop(shopName=shopName1, shopNumber=10, shopItem=ShopItem(shopItemName=shopItemName2))
    clone: Shop(shopName=shopName1, shopNumber=10, shopItem=ShopItem(shopItemName=shopItemName2))

    ------------------------------------------------------深拷贝------------------------------------------------------------------------------

    @Data
    public class ShopItem implements Cloneable {
    private String shopItemName;
    public ShopItem clone() throws CloneNotSupportedException {
    ShopItem clone = (ShopItem) super.clone();
    return clone;
    }
    }

    @Data
    public class Shop implements Cloneable {
        private String shopName;
    private Integer shopNumber;
    private ShopItem shopItem;

    public Shop(String shopName, Integer shopNumber, ShopItem shopItem) {
    this.shopName = shopName;
    this.shopNumber = shopNumber;
    this.shopItem = shopItem;
    }

    public static void main(String[] args) throws CloneNotSupportedException {
    ShopItem shopItem = new ShopItem();
    shopItem.setShopItemName("shopItemName1");
    Shop shop = new Shop("shopName1", 10, shopItem);
    Shop clone = shop.clone();
    System.out.println("shop: " + shop);
    System.out.println("clone: " + clone);
    System.out.println("-----------------");
    shopItem.setShopItemName("shopItemName2");
    System.out.println("shop: " + shop);
    System.out.println("clone: " + clone);
    }

    public Shop clone() throws CloneNotSupportedException {
    Shop shop = (Shop) super.clone();
    ShopItem clone = this.shopItem.clone();
    shop.setShopItem(clone);
    return shop;
    }
    }

    打印结果:

    shop: Shop(shopName=shopName1, shopNumber=10, shopItem=ShopItem(shopItemName=shopItemName1))
    clone: Shop(shopName=shopName1, shopNumber=10, shopItem=ShopItem(shopItemName=shopItemName1))
    -----------------
    shop: Shop(shopName=shopName1, shopNumber=10, shopItem=ShopItem(shopItemName=shopItemName2))
    clone: Shop(shopName=shopName1, shopNumber=10, shopItem=ShopItem(shopItemName=shopItemName1))

     ------------------------------------------------------------反序列化方式----------------------------------------------------------------

    @Data
    public class Shop implements Cloneable, Serializable {
    private String shopName;
    private Integer shopNumber;
    private ShopItem shopItem;

    public Shop(String shopName, Integer shopNumber, ShopItem shopItem) {
    this.shopName = shopName;
    this.shopNumber = shopNumber;
    this.shopItem = shopItem;
    }

    public static void main(String[] args) throws CloneNotSupportedException {
    ShopItem shopItem = new ShopItem();
    shopItem.setShopItemName("shopItemName1");
    Shop shop = new Shop("shopName1", 10, shopItem);
    Shop clone = shop.clone();
    System.out.println("shop: " + shop);
    System.out.println("clone: " + clone);
    System.out.println("-----------------");
    shopItem.setShopItemName("shopItemName2");
    System.out.println("shop: " + shop);
    System.out.println("clone: " + clone);
    }

    public Shop clone() {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try(ObjectOutputStream oos = new ObjectOutputStream(baos)) {
    oos.writeObject(this);
    } catch (IOException e) {
    e.printStackTrace();
    }

    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    try(ObjectInputStream oos = new ObjectInputStream(bais)) {
    Shop shop = (Shop) oos.readObject();
    return shop;
    } catch (IOException | ClassNotFoundException e) {
    e.printStackTrace();
    }
    return null;
    }
    }

    @Data
    public class ShopItem implements Cloneable, Serializable {
    private String shopItemName;

    // public ShopItem clone() throws CloneNotSupportedException {
    // ShopItem clone = (ShopItem) super.clone();
    // return clone;
    // }
    }

    打印结果:

    shop: Shop(shopName=shopName1, shopNumber=10, shopItem=ShopItem(shopItemName=shopItemName1))
    clone: Shop(shopName=shopName1, shopNumber=10, shopItem=ShopItem(shopItemName=shopItemName1))
    -----------------
    shop: Shop(shopName=shopName1, shopNumber=10, shopItem=ShopItem(shopItemName=shopItemName2))
    clone: Shop(shopName=shopName1, shopNumber=10, shopItem=ShopItem(shopItemName=shopItemName1))



  • 相关阅读:
    手机web页面调用手机QQ实现在线聊天的效果
    Java EE 6 最终草案暗示了平台的未来发展方向
    NetBeans 时事通讯(刊号 # 77 Oct 21, 2009)
    Java 依赖注射规范(JSR330)正式发布
    NetBeans IDE 6.8 里程碑 2 已经可用!
    Java 依赖注射规范(JSR330)正式发布
    对面向对象和面向过程的一些新理解
    NetBeans 时事通讯(刊号 # 76 Oct 13, 2009)
    Java EE 6 最终草案暗示了平台的未来发展方向
    “当前不会命中断点。源代码与原始版本不同”的问题的有效解决办法
  • 原文地址:https://www.cnblogs.com/ladeng19/p/15933653.html
Copyright © 2020-2023  润新知