辐射3全dlc整合版下载:设计模式之Prototype 原型模式
来源:百度文库 编辑:九乡新闻网 时间:2024/04/29 16:28:42
原型模式定义:
用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象.
Prototype模式允许一个对象再创建另外一个可定制的对象,根本无需知道任何如何创建的细节,工作原理是:通过将一个原型对象传给那个要发动创建的对象,这个要发动创建的对象通过请求原型对象拷贝它们自己来实施创建。
如何使用?
因为Java中的提供clone()方法来实现对象的克隆,所以Prototype模式实现一下子变得很简单.
以勺子为例:
public abstract class AbstractSpoon implements Cloneable{
String spoonName;
public void setSpoonName(String spoonName) {this.spoonName = spoonName;}
public String getSpoonName() {return this.spoonName;}
public Object clone()
{
Object object = null;
try {
object = super.clone();
} catch (CloneNotSupportedException exception) {
System.err.println("AbstractSpoon is not Cloneable");
}
return object;
}
}
有个具体实现(ConcretePrototype):
public class SoupSpoon extends AbstractSpoon
{
public SoupSpoon()
{
setSpoonName("Soup Spoon");
}
}
调用Prototype模式很简单:
AbstractSpoon spoon = new SoupSpoon();
AbstractSpoon spoon2 = spoon.clone();
当然也可以结合工厂模式来创建AbstractSpoon实例。
在Java中Prototype模式变成clone()方法的使用,由于Java的纯洁的面向对象特性,使得在Java中使用设计模式变得很自然,两者已经几乎是浑然一体了。这反映在很多模式上,如Interator遍历模式。
设计模式之Prototype 原型模式
设计模式6:Prototype Pattern (原型模式)
原型(Prototype)模式 != Object.clone()
常用设计模式之简单工厂模式
常用设计模式之观察者模式
常用设计模式之策略模式
设计模式之Builder 生成器模式
设计模式之Template 模板模式
设计模式之Observer 观察者模式
设计模式之Mediator 中介者模式
设计模式之Interpreter 解释器模式
设计模式之Composite 组成模式 树模式
设计模式之Bridge 桥接模式 Bridge模式
研磨设计模式之桥接模式-3
设计模式之Chain of Responsibility 职责链模式
GoF设计模式之十六 Iterator- -
薪酬模式设计探讨
薪酬模式设计探讨 -
数据库模式设计
设计模式概述1
GOF 23设计模式
常用设计模式收集
设计模式---代理模式(Proxy Pattern)
设计模式18:Visitor Pattern (访问者模式)