原型模式

 Prototype.java

 

  1. public class Prototype implements Cloneable { 
  2.      
  3.     private String name; 
  4.  
  5.     public String getName() { 
  6.         return name; 
  7.     } 
  8.  
  9.     public void setName(String name) { 
  10.         this.name = name; 
  11.     } 
  12.  
  13.     @Override 
  14.     protected Object clone() { 
  15.         // TODO Auto-generated method stub 
  16.         try { 
  17.             return super.clone(); 
  18.         } catch (CloneNotSupportedException e) { 
  19.             // TODO Auto-generated catch block 
  20.             e.printStackTrace(); 
  21.             return null
  22.         } 
  23.     } 
  24.      

Concreateprototype.java

 

  1. public class Concreateprototype extends Prototype{ 
  2.      
  3.     public Concreateprototype(String name) { 
  4.         // TODO Auto-generated constructor stub 
  5.         setName(name); 
  6.     } 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章