标签:throw hid The zhang 代码示例 zha 相等 str his
原型模式包含如下角色:
? Prototype:原型
声明克隆自己的接口
? ConcretePrototype:具体原型
实现克隆自己的操作
? Client:客户
要求原型克隆自己,生成新的对象
/** * Prototype Class * 原型类 */ public class Cookie implements Cloneable { @Override public Object clone() throws CloneNotSupportedException { //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 * 具体原型 **/ public class CoconutCookie extends Cookie { } /** * Client Class * 客户类 **/ public class CookieMachine { private Cookie cookie;//cookie必须是可复制的 public CookieMachine(Cookie cookie) { this.cookie = cookie; } public Cookie makeCookie() { try { return (Cookie) cookie.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } return null; } public static void main(String args[]) { Cookie tempCookie = null; Cookie prot = new CoconutCookie(); CookieMachine cm = new CookieMachine(prot); //设置原型 for (int i = 0; i < 100; i++) tempCookie = cm.makeCookie();//通过复制原型返回多个cookie } }
标签:throw hid The zhang 代码示例 zha 相等 str his
原文地址:https://www.cnblogs.com/756623607-zhang/p/9206805.html