對象的複製——原型模式(二)

來源:互聯網
上載者:User
7.3 完整解決方案

      Sunny公司開發人員決定使用原型模式來實現工作周報的快速建立,快速建立工作周報結構圖7-3所示:

圖7-3 快速建立工作周報結構圖

      在圖7-3中,WeeklyLog充當具體原型類,Object類充當抽象原型類,clone()方法為原型方法。WeeklyLog類的代碼如下所示:

//工作周報WeeklyLog:具體原型類,考慮到代碼的可讀性和易理解性,只列出部分與模式相關的核心代碼

class WeeklyLog implements Cloneable

{

       private  String name;

       private  String date;

       private  String content;

       public  void setName(String name) {

              this.name  = name;

       }

       public  void setDate(String date) {

              this.date  = date;

       }

       public  void setContent(String content) {

              this.content  = content;

       }

       public  String getName() {

              return  (this.name);

       }

       public  String getDate() {

              return  (this.date);

       }

       public  String getContent() {

              return  (this.content);

       }

     //複製方法clone(),此處使用Java語言提供的複製機制

       public WeeklyLog clone()

       {

              Object obj = null;

              try

              {

                     obj = super.clone();

                     return (WeeklyLog)obj;     

              }

              catch(CloneNotSupportedException e)

              {

                     System.out.println("不支援複製!");

                     return null;

              }

       }

}

編寫如下用戶端測試代碼:

class Client

{

       public  static void main(String args[])

       {

             
WeeklyLog log_previous = new WeeklyLog();  //
建立原型對象

              log_previous.setName("張無忌");

              log_previous.setDate("第12周");

              log_previous.setContent("這周工作很忙,每天加班!");

             

              System.out.println("****周報****");

              System.out.println("周次:" +  log_previous.getDate());

              System.out.println("姓名:" +  log_previous.getName());

              System.out.println("內容:" +  log_previous.getContent());

              System.out.println("--------------------------------");

             

              WeeklyLog  log_new;

              log_new  = log_previous.clone(); //調用複製方法建立複製對象

              log_new.setDate("第13周");

              System.out.println("****周報****");

              System.out.println("周次:" + log_new.getDate());

              System.out.println("姓名:" + log_new.getName());

              System.out.println("內容:" + log_new.getContent());

       }

}

      編譯並運行程式,輸出結果如下:

****周報****

周次:第12周

姓名:張無忌

內容:這周工作很忙,每天加班!

--------------------------------

****周報****

周次:第13周

姓名:張無忌

內容:這周工作很忙,每天加班!

      通過已建立的工作周報可以快速建立新的周報,然後再根據需要修改周報,無須再從頭開始建立。原型模式為工作流程系統中任務單的快速產生提供了一種解決方案。

 

思考

如果在Client類的main()函數中增加如下幾條語句:

System.out.println(log_previous == log_new);

System.out.println(log_previous.getDate() == log_new.getDate());

System.out.println(log_previous.getName() == log_new.getName());

System.out.println(log_previous.getContent() == log_new.getContent());

預測這些語句的輸出結果。

【作者:劉偉
http://blog.csdn.net/lovelion】

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.