名称:Builder
中文名:生成器
类型:创建型
简介:首先我要说:又一个相似、又一个
充分的OOP多态的利用.
不错,在体现一个接口多种实现上,
相似于Bridge.然它还有一个最大的
个性:那实现的接口签名被分解成多步的流程式.
对,就是这里.这样的方式有利于复杂对象(产品)
的精细控制.自然,对于较大的工程,对同样处理
步骤的分离也是指导思想之一.那么结合
起来讲:它通过流程处理签名
接口能精细的产生不同的复杂对象(产品),并可
自然的复用同处理.
[C#]
// created on 2003-3-14 at 10:13
//member:
//builder + concretebuilder + director + product
/*
interface builder{
string fook_(string m_STR1);
string fook__(string m_STR2);
string fook___(string m_STR3);
}
*/
using System;
class product_{
public string _str;
public product_(){
_str = "product_";
}
}
class builder{
public product_ m_OBJREF;
public builder(){
m_OBJREF = new product_();
}
virtual public product_ getproduct(){
return m_OBJREF;
}
virtual public void fook_(){
m_OBJREF._str = m_OBJREF._str+" bulider_fook_ ";
}
virtual public void fook__(){
m_OBJREF._str = m_OBJREF._str+" builder_fook__ ";
}
virtual public void fook___(){
m_OBJREF._str = m_OBJREF._str+" buidler_fook__ ";
}
}
class concretebuilder1:builder{
override public void fook_(){
m_OBJREF._str = m_OBJREF._str+" concreatebuilder1_fook_ ";
}
override public void fook___(){
m_OBJREF._str = m_OBJREF._str+" concreatebuilder1_fook___ ";
}
override public product_ getproduct(){return m_OBJREF;}
}
class concretebuilder2:builder{
override public void fook_(){
m_OBJREF._str = m_OBJREF._str+" concreatebuilder2_fook_ ";
}
override public void fook___(){
m_OBJREF._str = m_OBJREF._str+" concreatebuilder2_fook___ ";
}
override public product_ getproduct(){return m_OBJREF;}
}
class director{
public void BUILDPROCESS(builder m_OBJREFprocess){
m_OBJREFprocess.fook_();
m_OBJREFprocess.fook__();
m_OBJREFprocess.fook___();
}
}
class builder_test{
public static void Main()
{
director m_OBJdirector = new director();
concretebuilder1 m_OBJ1 = new concretebuilder1();
m_OBJdirector.BUILDPROCESS(m_OBJ1);
product_ m_OBJproduct_ = m_OBJ1.getproduct();
concretebuilder2 m_OBJ2 = new concretebuilder2();
m_OBJdirector.BUILDPROCESS(m_OBJ2);
product_ m_OBJproduct__ = m_OBJ2.getproduct();
Console.WriteLine(m_OBJproduct_._str);
Console.WriteLine(m_OBJproduct__._str);
}
}
简评:
1.我们建builder时,并未使用interface或者abstract class.
是为了the same transactor.
2.由Dirctor指导流程.而且Dirctor自己也不知道产生的产品是
什么.这样就将产品的信息完全隐藏.
---------------------------------------------------------------------
声明:本程式重在演示C#基本用法
不考虑在实际环境中应用带来
其它问题的情况.
如:多线程.
[用设计模式锻炼C#系列]之Singleton、Bridge
-----------------------------------------------------------------------------------