更多文章
更多精彩文章
结构
描述原型设计模式的UML类图
实现
Java
/** Prototype Class **/publicclassCookieimplementsCloneable{publicObjectclone()throwsCloneNotSupportedException{//In an actual implementation of this pattern you would now attach references to//the expensive to produce parts from the copies that are held inside the prototype.return(Cookie)super.clone();}}/** Concrete Prototypes to clone **/publicclassCoconutCookieextendsCookie{}/** Client Class**/publicclassCookieMachine{privateCookiecookie;//cookie必须是可复制的publicCookieMachine(Cookiecookie){this.cookie=cookie;}publicCookiemakeCookie(){try{return(Cookie)cookie.clone();}catch(CloneNotSupportedExceptione){e.printStackTrace();}return;}publicstaticvoidmain(Stringargs[]){CookietempCookie=;Cookieprot=newCoconutCookie();CookieMachinecm=newCookieMachine(prot);//设置原型for(inti=0;i<100;i++)tempCookie=cm.makeCookie();//通过复制原型返回多个cookie }}
引用
来源
Gamma, Erich; Helm, Richard; Johnson, Ralph; Vlissides, John. Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley. 1994. ISBN 0-201-63361-2.
参见
函数原型
免责声明:以上内容版权归原作者所有,如有侵犯您的原创版权请告知,我们将尽快删除相关内容。感谢每一位辛勤著写的作者,感谢每一位的分享。
{{item.time}} {{item.replyListShow ? '收起' : '展开'}}评论 {{curReplyId == item.id ? '取消回复' : '回复'}}