設計模式-原型模式(06),設計模式原型
定義
原型(Prototype Pattern)是一個簡單的設計模式。原型模式的英文原話是:Specify the kind of objects to create using a prototypical instance,and create new objects by copying this prototype.意思是:用原型執行個體指定建立對象的種類,並且通過複製這些原型建立新的對象。
原型模式有三種角色:
1.客戶(Client)角色:該角色提出建立對象的請求。
2.抽象原型(Prototype):該角色是一個抽象角色,通常由一個java介面或抽象類別實現,給出所有的具體原型類所需要的介面。
3.具體原型(Concrete Prototype)角色:該角色是被複製的對象,必須實現抽象原型介面。
java中內建了複製機制。object類具有一個clone()方法,能夠實現對對象的複製,是一個類支援複製只需要兩步:
1.實現Cloneable介面。
2.覆蓋Object的clone()方法,完成對象的複製操作,通常只需要調用Object的clone()方法即可。為了使外部能夠調用此類的clone()方法,可以將存取修飾詞改為public。
/** * 抽象原型角色(Prototype) * 給出具體原型類複製所需要的介面 */public interface Prototype { //複製方法 Prototype clone();}/** * 具體原型工廠類 * */public class ConcretePrototype implements Prototype { @Override public Prototype clone() { try { return (Prototype)super.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); return null; } }}public class Client { public void operation(Prototype example) { //得到example Prototype prototype = example.clone(); }}原型模式的優點
1.效能優良:原型模式是對記憶體中二進位流的拷貝,要比直接new一個對象效能好,特別是當一個迴圈體內產生大量的對象時,原型模式可以更好地體現其優點。
2.逃避建構函式的約束。這既是優點也是缺點,直接在記憶體中拷貝對象,建構函式是不會執行的,因此減少了約束,不過這一點需要在實際應用時進行權衡考慮。
原型模式的使用情境
1.資源最佳化情境,類初始化時需要消化非常多的資源,這個資源套件括資料、硬體資源等。
2.效能和安全要求的情境,如果通過new產生一個對象需要非常繁瑣的資料準備和存取權限,則可以使用原型模式。
3.一個對象多個修改者的情境,一個對象需要提供給其他對象訪問,而且各個調用者可能都需要修改其值,可以考慮使用原型模式拷貝多個對象供調用者使用。
在實際項目中,原型模式很少單獨出現,一般是和Factory 方法模式一起出現。原型模式通過clone()方法建立一個對象,然後由Factory 方法提供給調用者。
例子
/** * 實現clone介面,實現了clone方法,是實現複製的關鍵 */public class Mail implements Cloneable { //收件者 private String receiver; //郵件標題 private String subject; //稱謂 private String appellation; //郵件內容 private String contxt; //尾部 private String tail; //建構函式 public Mail(String subject,String contxt) { this.subject = subject; this.contxt = contxt; } //複製方法 public Mail clone() { Mail mail = null; try { mail=(Mail) super.clone(); System.out.println(super.toString()); //super指的是被調用的那個對象 } catch (CloneNotSupportedException e) { e.printStackTrace(); } return mail; }}/** * 調用 */public class SendMailDemo { Map students = new LinkedHashMap(); public static void main(String[] args) { //建立一個原型mail Mail mail = new Mail("郵件標題", "郵件內容"); mail.setTail("2017-11-20"); SendMailDemo sendMailDemo = new SendMailDemo(); //擷取所有學生 Map students=sendMailDemo.getStudent(); for (Object name : students.keySet()) { //複製郵件 Mail cloneMail = mail.clone(); cloneMail.setAppellation(name.toString()); cloneMail.setReceiver(students.get(name).toString()); sendMailDemo.sendMail(cloneMail); } } public Map getStudent(){ students.put("studentone", "1@foxmail.com"); students.put("studenttwo", "2@foxmail.com"); students.put("studentthree", "3@foxmail.com"); students.put("studentfour", "4@foxmail.com"); students.put("studentfive", "5@foxmail.com"); students.put("studentsix", "6@foxmail.com"); students.put("studentseven", "7@foxmail.com"); return students; } public void sendMail(Mail mail){ System.out.println("標題:"+mail.getSubject()+"\t收件者郵箱:"+mail.getReceiver()+"\t本文:"+mail.getAppellation()+mail.getContxt()+"\t...已發送"); }}
源碼