中国代加工网站,邢台做网站优化费用,色多多导入百媚导航,wordpress小工具里页面设计模式 原型模式创新设计模式之一是原型设计模式 。 尽管原型是创造模式#xff0c;但它在概念上与其他模式有所区别。 我的意思是原型在某种意义上创造了自己。 我将在下面解释。 原型模式的所有魔力都基于Java Object的clone#xff08;#xff09;方法。 因此#x… 设计模式 原型模式 创新设计模式之一是原型设计模式 。 尽管原型是创造模式但它在概念上与其他模式有所区别。 我的意思是原型在某种意义上创造了自己。 我将在下面解释。 原型模式的所有魔力都基于Java Object的clone方法。 因此让我们考虑一个用法示例然后我将尝试找出该模式的利弊。 上面的类图向我们展示了该模式的基本含义。 抽象类或接口可以充当原型的角色。 注意原型必须扩展Cloneable接口。 这是因为原型的具体实现将调用clone方法。 实现接口的特定类扩展了抽象类必须包含将在克隆操作的帮助下返回其自身副本的方法。 在我的示例中我将Unicellular接口声明为原型并将Amoeba类声明为其实现 public interface Unicellular extends Cloneable {public Unicellular reproduce();}public class Amoeba implements Unicellular {public Unicellular reproduce() {Unicellular amoeba null;try {amoeba (Unicellular) super.clone();} catch (CloneNotSupportedException e) {e.printStackTrace();}return amoeba;}public String toString() {return Bla bla bla its a new amoeba...;}} 示范 ...public static void main(String[] args) {Unicellular amoeba new Amoeba();List Unicellular amoebaList new ArrayList Unicellular ();amoebaList.add(amoeba.reproduce());amoebaList.add(amoeba.reproduce());amoebaList.add(amoeba.reproduce());for (Unicellular a : amoebaList)System.out.println(a);}
... 结果 Bla bla bla it’s a new amoeba…
Bla bla bla it’s a new amoeba…
Bla bla bla it’s a new amoeba… 利弊如何 实际上我不知道该说些什么因为我从未遇到过适当应用原型模式的情况。 也许在某些情况下当您不需要显式调用构造函数或系统不需要依赖于对象创建方式时。 参考 设计模式来自我们JCG合作伙伴 Alexey Zvolinskiy的原型 位于Fruzenshtein的注释博客中。 翻译自: https://www.javacodegeeks.com/2013/06/design-patterns-prototype.html设计模式 原型模式