首页 > 编程语言 > 详细

Java-原型模式

时间:2020-04-08 21:49:19      阅读:53      评论:0      收藏:0      [点我收藏+]
// 原文:https://www.cnblogs.com/programmerkaixin/p/10969745.html
public class PrototypeClientTest {

    public static void main(String[] args) throws CloneNotSupportedException {

        Invoice invoice = new Invoice();// 电子发票

        invoice.setTicketNo(1);// 票号
        invoice.setColor("blue");// 发票联颜色
        invoice.setCompanySeal(new Seal());// 公司印章
        invoice.setEffective(false);// 是否有效
        // 1.测试浅克隆
//        Invoice invoiceClone = (Invoice) invoice.getShallowCloneInstance();
        // 2.测试深克隆
        Invoice invoiceClone = (Invoice) invoice.getDeepCloneInstance(invoice);
//
        // -+-+-+-+-++-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-++-+-+-+-+-+-+-+-+-+-+--++-+--+-++-+-+-+-+-+-+-+-
        System.out.println("1.验证int类型是否相等:");
        System.out.printf("\t票号:%s\t", invoice.getTicketNo());
        System.out.printf("\t克隆出来的票号:%s\t", invoiceClone.getTicketNo());
        System.out.printf("\t验证int类型:%s\n", (invoice.getTicketNo() == invoiceClone.getTicketNo()));

        System.out.println("2.验证String类型是否相等:");
        System.out.printf("\t发票联颜色:%s\t", invoice.getColor().hashCode());
        System.out.printf("\t克隆出来的发票联颜色:%s\t", invoiceClone.getColor().hashCode());
        System.out.printf("\t验证String类型:%s\n", (invoiceClone.getColor().equals(invoice.getColor())));

        System.out.println("3.验证引用数据类型是否相等:");
        System.out.printf("\t公司印章:%s\t", invoice.getCompanySeal().hashCode());
        System.out.printf("\t克隆出来的公司印章:%s\t", invoiceClone.getCompanySeal().hashCode());
        System.out.printf("\t验证引用数据类型:%s\n", (invoice.getCompanySeal() == invoiceClone.getCompanySeal()));

        System.out.println("4.验证boolean类型是否相等:");
        System.out.printf("\t是否有效:%s\t", invoice.getEffective().hashCode());
        System.out.printf("\t克隆出来的是否有效:%s\t", invoiceClone.getEffective().hashCode());
        System.out.printf("\t验证boolean类型:%s\n\n", (invoice.getEffective().equals(invoiceClone.getEffective())));


        // -+-+-+-+-++-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-++-+-+-+-+-+-+-+-+-+-+--++-+--+-++-+-+-+-+-+-+-+-
        invoice.setTicketNo(2);// 票号
        System.out.printf("1.修改后验证int类型:\n\t%s\n\t%s\n", invoice, invoiceClone);
        invoice.setColor("green");// 发票联颜色
        System.out.printf("2.修改后验证String类型:\n\t%s\n\t%s\n", invoice, invoiceClone);
        invoice.getCompanySeal().setName("迷心兔");// 公司印章名称
        System.out.printf("3.修改后验证引用数据类型:\n\t%s\n\t%s\n", invoice, invoiceClone);
        invoice.setEffective(true);// 是否有效
        System.out.printf("4.修改后验证boolean类型包装类:\n\t%s\n\t%s\n", invoice, invoiceClone);
    }
}

  

 

## 浅克隆

1.验证int类型是否相等:
    票号:1        克隆出来的票号:1        验证int类型:true
2.验证String类型是否相等:
    发票联颜色:3027034        克隆出来的发票联颜色:3027034        验证String类型:true
3.验证引用数据类型是否相等:
    公司印章:1342443276        克隆出来的公司印章:1342443276        验证引用数据类型:true
4.验证boolean类型是否相等:
    是否有效:1237        克隆出来的是否有效:1237        验证boolean类型:true

1.修改后验证int类型:
    电子发票: {ticketHeader=‘null‘, ticketNo=2, name=‘null‘, color=‘blue‘, company=‘null‘, companySeal=电子印章: {name=‘null‘, image=null}, supervisedSeal=null, effective=false}
    电子发票: {ticketHeader=‘null‘, ticketNo=1, name=‘null‘, color=‘blue‘, company=‘null‘, companySeal=电子印章: {name=‘null‘, image=null}, supervisedSeal=null, effective=false}
2.修改后验证String类型:
    电子发票: {ticketHeader=‘null‘, ticketNo=2, name=‘null‘, color=‘green‘, company=‘null‘, companySeal=电子印章: {name=‘null‘, image=null}, supervisedSeal=null, effective=false}
    电子发票: {ticketHeader=‘null‘, ticketNo=1, name=‘null‘, color=‘blue‘, company=‘null‘, companySeal=电子印章: {name=‘null‘, image=null}, supervisedSeal=null, effective=false}
3.修改后验证引用数据类型:
    电子发票: {ticketHeader=‘null‘, ticketNo=2, name=‘null‘, color=‘green‘, company=‘null‘, companySeal=电子印章: {name=‘迷心兔‘, image=null}, supervisedSeal=null, effective=false}
    电子发票: {ticketHeader=‘null‘, ticketNo=1, name=‘null‘, color=‘blue‘, company=‘null‘, companySeal=电子印章: {name=‘迷心兔‘, image=null}, supervisedSeal=null, effective=false}
4.修改后验证boolean类型包装类:
    电子发票: {ticketHeader=‘null‘, ticketNo=2, name=‘null‘, color=‘green‘, company=‘null‘, companySeal=电子印章: {name=‘迷心兔‘, image=null}, supervisedSeal=null, effective=true}
    电子发票: {ticketHeader=‘null‘, ticketNo=1, name=‘null‘, color=‘blue‘, company=‘null‘, companySeal=电子印章: {name=‘迷心兔‘, image=null}, supervisedSeal=null, effective=false}

  

 

 

## 深克隆
1.验证int类型是否相等:
  票号:1    克隆出来的票号:1   验证int类型:true
2.验证String类型是否相等:
  发票联颜色:3027034   克隆出来的发票联颜色:3027034    验证String类型:true
3.验证引用数据类型是否相等:
  公司印章:1494279232   克隆出来的公司印章:1177096266    验证引用数据类型:false
4.验证boolean类型是否相等:
  是否有效:1237   克隆出来的是否有效:1237    验证boolean类型:true

1.修改后验证int类型:
  电子发票: {ticketHeader=‘null‘, ticketNo=2, name=‘null‘, color=‘blue‘, company=‘null‘, companySeal=电子印章: {name=‘null‘, image=null}, supervisedSeal=null, effective=false}
  电子发票: {ticketHeader=‘null‘, ticketNo=1, name=‘null‘, color=‘blue‘, company=‘null‘, companySeal=电子印章: {name=‘null‘, image=null}, supervisedSeal=null, effective=false}
2.修改后验证String类型:
  电子发票: {ticketHeader=‘null‘, ticketNo=2, name=‘null‘, color=‘green‘, company=‘null‘, companySeal=电子印章: {name=‘null‘, image=null}, supervisedSeal=null, effective=false}
  电子发票: {ticketHeader=‘null‘, ticketNo=1, name=‘null‘, color=‘blue‘, company=‘null‘, companySeal=电子印章: {name=‘null‘, image=null}, supervisedSeal=null, effective=false}
3.修改后验证引用数据类型:
  电子发票: {ticketHeader=‘null‘, ticketNo=2, name=‘null‘, color=‘green‘, company=‘null‘, companySeal=电子印章: {name=‘迷心兔‘, image=null}, supervisedSeal=null, effective=false}
  电子发票: {ticketHeader=‘null‘, ticketNo=1, name=‘null‘, color=‘blue‘, company=‘null‘, companySeal=电子印章: {name=‘null‘, image=null}, supervisedSeal=null, effective=false}
4.修改后验证boolean类型包装类:
  电子发票: {ticketHeader=‘null‘, ticketNo=2, name=‘null‘, color=‘green‘, company=‘null‘, companySeal=电子印章: {name=‘迷心兔‘, image=null}, supervisedSeal=null, effective=true}
  电子发票: {ticketHeader=‘null‘, ticketNo=1, name=‘null‘, color=‘blue‘, company=‘null‘, companySeal=电子印章: {name=‘null‘, image=null}, supervisedSeal=null, effective=false}
  

  

Java-原型模式

原文:https://www.cnblogs.com/mixintu/p/12662777.html

(1)
(1)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!