• 设计模式原型模式


    -------------------------------浅拷贝-----------------------------------------
    @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))



  • 相关阅读:
    Scalding初探之番外篇:Mac OS下的安装
    Scalding初探之二:动手来做做小实验
    没有好看的 Terminal 怎么能够快乐地写代码
    Scalding初探之一:基于Scala的Hadoop利器
    Scala初探:新潮的函数式面向对象语言
    【题解】【数组】【Prefix Sums】【Codility】Genomic Range Query
    【题解】【数组】【Prefix Sums】【Codility】Passing Cars
    hibernate基于注解实现映射关系的配置
    decimalFormat
    我对shiro的初步认识
  • 原文地址:https://www.cnblogs.com/ladeng19/p/15933653.html
Copyright © 2020-2023  润新知