Factory Method Pattern
模板模式的衍生品?
以Template Method Pattern 架构获取产生对象实例的工厂就是Factory Method Pattern。
工厂方法模式在父类规定对象的创建方法,但并没有深入到较具体的类名。所有具体的完整内容 都放在子类。根据这个原则,我们可以大致分成产生对象实例的大纲(框架)和实际生产对象实例的类两方面。
场景模拟
以一个工厂进厂打工为原型,工人们进厂须先登记信息,由所登记的信息进入创建工卡,工人们每天进厂必须使用工卡打卡,开始一天的劳动。以此为例,进行编程,设计UML图如下:
程序实现
抽象类Product定义抽象方法create ()表示创建用工card 信息和use () 表示用户使用card
抽象类Factory实现方法create 的抽象类,这里就是我们上文所说的为什么是模板方法模式衍生品。和定义抽象方法createProduct()、抽象方法registerProduct() 交由子类负责完成 。
类IDcard 继承自Product 实现方法use 和create
类IDCardFactory 继承 自Factory 实现方法createProduct、registerProduct
Product 抽象类
public
abstract
class
Product {
public abstract String create();
public abstract String use();
}
public abstract String create();
public abstract String use();
}
Factory 抽象类
public
abstract
class
Factory {
public final Product create(String owner) {
Product p = createProduct(owner);
registerProduct(p);
return p;
}
protected abstract Product createProduct(String owner);
protected abstract void registerProduct(Product product);
}
public final Product create(String owner) {
Product p = createProduct(owner);
registerProduct(p);
return p;
}
protected abstract Product createProduct(String owner);
protected abstract void registerProduct(Product product);
}
IDCard 类
public
class
IDCard extends Product{
private String owner;
public IDCard(String owner) {
// TODO Auto-generated constructor stub
this .owner = owner;
}
@Override
public String use() {
// TODO Auto-generated method stub
return " 使用 " + owner + " 的卡 " ;
}
public String getOwner(){
return owner;
}
@Override
public String create() {
// TODO Auto-generated method stub
return " 建立 " + owner + " 的卡 " ;
}
}
private String owner;
public IDCard(String owner) {
// TODO Auto-generated constructor stub
this .owner = owner;
}
@Override
public String use() {
// TODO Auto-generated method stub
return " 使用 " + owner + " 的卡 " ;
}
public String getOwner(){
return owner;
}
@Override
public String create() {
// TODO Auto-generated method stub
return " 建立 " + owner + " 的卡 " ;
}
}
IDCardFactory 类
public
class
IDCardFactory extends Factory{
private Vector < String > owners = new Vector < String > ();
@Override
protected Product createProduct(String owner) {
// TODO Auto-generated method stub
return new IDCard(owner);
}
@Override
protected void registerProduct(Product product) {
// TODO Auto-generated method stub
owners.add(((IDCard)product).getOwner());
}
public Vector < String > getOwners(){
return owners;
}
}
private Vector < String > owners = new Vector < String > ();
@Override
protected Product createProduct(String owner) {
// TODO Auto-generated method stub
return new IDCard(owner);
}
@Override
protected void registerProduct(Product product) {
// TODO Auto-generated method stub
owners.add(((IDCard)product).getOwner());
}
public Vector < String > getOwners(){
return owners;
}
}
界面代码实现 FatoryMethodActivity:
public
class
FatoryMethodActivity extends Activity implements OnClickListener {
/* * Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
((Button) findViewById(R.id.Button01)).setOnClickListener( this );
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Factory factory = new IDCardFactory();
Product card1 = factory.create( " terry " );
Product card2 = factory.create( " paul " );
Product card3 = factory.create( " jim " );
((EditText) findViewById(R.id.EditText01)).setText(card1.create() + " , "
+ card1.use() + " \n " + card2.create() + " , " + card2.use() + " \n "
+ card3.create() + " , " + card3.use());
}
}
/* * Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
((Button) findViewById(R.id.Button01)).setOnClickListener( this );
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Factory factory = new IDCardFactory();
Product card1 = factory.create( " terry " );
Product card2 = factory.create( " paul " );
Product card3 = factory.create( " jim " );
((EditText) findViewById(R.id.EditText01)).setText(card1.create() + " , "
+ card1.use() + " \n " + card2.create() + " , " + card2.use() + " \n "
+ card3.create() + " , " + card3.use());
}
}
最终实现效果:
代码下载:
本文转自 terry_龙 51CTO博客,原文链接:http://blog.51cto.com/terryblog/609318,如需转载请自行联系原作者