原文:乐在其中设计模式(C#) - 建造者模式(Builder Pattern)
乐在其中设计模式(C#) - 建造者模式(Builder Pattern)
作者:webabcd
介绍
将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。
示例
用同样的构建过程创建Sql和Xml的Insert()方法和Get()方法。
MessageModel
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.Builder
{
/**//// <summary>
/// Message实体类
/// </summary>
public class MessageModel
{
/**//// <summary>
/// 构造函数
/// </summary>
/// <param name="msg">Message内容</param>
/// <param name="pt">Message发布时间</param>
public MessageModel(string msg, DateTime pt)
{
this._message = msg;
this._publishTime = pt;
}
private string _message;
/**//// <summary>
/// Message内容
/// </summary>
public string Message
{
get { return _message; }
set { _message = value; }
}
private DateTime _publishTime;
/**//// <summary>
/// Message发布时间
/// </summary>
public DateTime PublishTime
{
get { return _publishTime; }
set { _publishTime = value; }
}
}
}
using System.Collections.Generic;
using System.Text;
namespace Pattern.Builder
{
/**//// <summary>
/// Message实体类
/// </summary>
public class MessageModel
{
/**//// <summary>
/// 构造函数
/// </summary>
/// <param name="msg">Message内容</param>
/// <param name="pt">Message发布时间</param>
public MessageModel(string msg, DateTime pt)
{
this._message = msg;
this._publishTime = pt;
}
private string _message;
/**//// <summary>
/// Message内容
/// </summary>
public string Message
{
get { return _message; }
set { _message = value; }
}
private DateTime _publishTime;
/**//// <summary>
/// Message发布时间
/// </summary>
public DateTime PublishTime
{
get { return _publishTime; }
set { _publishTime = value; }
}
}
}
SqlMessage
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.Builder
{
/**//// <summary>
/// Sql方式操作Message
/// </summary>
public class SqlMessage
{
/**//// <summary>
/// 获取Message
/// </summary>
/// <returns></returns>
public List<MessageModel> Get()
{
List<MessageModel> l = new List<MessageModel>();
l.Add(new MessageModel("SQL方式获取Message", DateTime.Now));
return l;
}
/**//// <summary>
/// 插入Message
/// </summary>
/// <param name="mm">Message实体对象</param>
/// <returns></returns>
public bool Insert(MessageModel mm)
{
// 代码略
return true;
}
}
}
using System.Collections.Generic;
using System.Text;
namespace Pattern.Builder
{
/**//// <summary>
/// Sql方式操作Message
/// </summary>
public class SqlMessage
{
/**//// <summary>
/// 获取Message
/// </summary>
/// <returns></returns>
public List<MessageModel> Get()
{
List<MessageModel> l = new List<MessageModel>();
l.Add(new MessageModel("SQL方式获取Message", DateTime.Now));
return l;
}
/**//// <summary>
/// 插入Message
/// </summary>
/// <param name="mm">Message实体对象</param>
/// <returns></returns>
public bool Insert(MessageModel mm)
{
// 代码略
return true;
}
}
}
XmlMessage
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.Builder
{
/**//// <summary>
/// Xml方式操作Message
/// </summary>
public class XmlMessage
{
/**//// <summary>
/// 获取Message
/// </summary>
/// <returns></returns>
public List<MessageModel> Get()
{
List<MessageModel> l = new List<MessageModel>();
l.Add(new MessageModel("XML方式获取Message", DateTime.Now));
return l;
}
/**//// <summary>
/// 插入Message
/// </summary>
/// <param name="mm">Message实体对象</param>
/// <returns></returns>
public bool Insert(MessageModel mm)
{
// 代码略
return true;
}
}
}
using System.Collections.Generic;
using System.Text;
namespace Pattern.Builder
{
/**//// <summary>
/// Xml方式操作Message
/// </summary>
public class XmlMessage
{
/**//// <summary>
/// 获取Message
/// </summary>
/// <returns></returns>
public List<MessageModel> Get()
{
List<MessageModel> l = new List<MessageModel>();
l.Add(new MessageModel("XML方式获取Message", DateTime.Now));
return l;
}
/**//// <summary>
/// 插入Message
/// </summary>
/// <param name="mm">Message实体对象</param>
/// <returns></returns>
public bool Insert(MessageModel mm)
{
// 代码略
return true;
}
}
}
Operation
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
namespace Pattern.Builder
{
/**//// <summary>
/// 操作(Product)
/// </summary>
public class Operation
{
private string _type;
private Dictionary<string, string> _dictionary;
/**//// <summary>
/// 构造函数
/// </summary>
/// <param name="type">产品类型</param>
public Operation(string type)
{
_dictionary = new Dictionary<string, string>();
this._type = type;
}
/**//// <summary>
/// 索引器
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public string this[string key]
{
get { return _dictionary[key]; }
set { _dictionary[key] = value; }
}
/**//// <summary>
/// 获得结果
/// </summary>
/// <returns></returns>
public string GetResult()
{
Assembly assembly = Assembly.Load("Pattern.Builder");
MethodInfo methodGet ]);
]), null);
List<MessageModel> m = (List<MessageModel>)objGet;
MethodInfo methodInsert ]);
])) });
bool b = (bool)objInsert;
].PublishTime.ToString() + "<br />";
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Reflection;
namespace Pattern.Builder
{
/**//// <summary>
/// 操作(Product)
/// </summary>
public class Operation
{
private string _type;
private Dictionary<string, string> _dictionary;
/**//// <summary>
/// 构造函数
/// </summary>
/// <param name="type">产品类型</param>
public Operation(string type)
{
_dictionary = new Dictionary<string, string>();
this._type = type;
}
/**//// <summary>
/// 索引器
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public string this[string key]
{
get { return _dictionary[key]; }
set { _dictionary[key] = value; }
}
/**//// <summary>
/// 获得结果
/// </summary>
/// <returns></returns>
public string GetResult()
{
Assembly assembly = Assembly.Load("Pattern.Builder");
MethodInfo methodGet ]);
]), null);
List<MessageModel> m = (List<MessageModel>)objGet;
MethodInfo methodInsert ]);
])) });
bool b = (bool)objInsert;
].PublishTime.ToString() + "<br />";
}
}
}
AbstractMessageBuilder
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.Builder
{
/**//// <summary>
/// 抽象Message建造者(Builder)
/// </summary>
public abstract class AbstractMessageBuilder
{
/**//// <summary>
/// 构造函数
/// </summary>
/// <param name="type">产品类型</param>
public AbstractMessageBuilder(string type)
{
this._operation = new Operation(type);
}
/**//// <summary>
/// 操作(Product)
/// </summary>
protected Operation _operation;
/**//// <summary>
/// 操作(Product)
/// </summary>
public Operation Operation
{
get { return _operation; }
set { _operation = value; }
}
/**//// <summary>
/// 建造插入方法
/// </summary>
/// <param name="mm"></param>
abstract public void BuilderInsert(MessageModel mm);
/**//// <summary>
/// 建造获取方法
/// </summary>
abstract public void BuilderGet();
}
}
using System.Collections.Generic;
using System.Text;
namespace Pattern.Builder
{
/**//// <summary>
/// 抽象Message建造者(Builder)
/// </summary>
public abstract class AbstractMessageBuilder
{
/**//// <summary>
/// 构造函数
/// </summary>
/// <param name="type">产品类型</param>
public AbstractMessageBuilder(string type)
{
this._operation = new Operation(type);
}
/**//// <summary>
/// 操作(Product)
/// </summary>
protected Operation _operation;
/**//// <summary>
/// 操作(Product)
/// </summary>
public Operation Operation
{
get { return _operation; }
set { _operation = value; }
}
/**//// <summary>
/// 建造插入方法
/// </summary>
/// <param name="mm"></param>
abstract public void BuilderInsert(MessageModel mm);
/**//// <summary>
/// 建造获取方法
/// </summary>
abstract public void BuilderGet();
}
}
SqlMessageBuilder
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.Builder
{
/**//// <summary>
/// SqlMessage建造者(ConcreteBuilder)
/// </summary>
public class SqlMessageBuilder : AbstractMessageBuilder
{
/**//// <summary>
/// 构造函数
/// </summary>
public SqlMessageBuilder()
: base("sql")
{
}
/**//// <summary>
/// 建造Sql插入方法
/// </summary>
/// <param name="mm">Message实体对象</param>
public override void BuilderInsert(MessageModel mm)
{
base._operation["insert"] = "SqlMessage|Insert|" + mm.Message + "|" + mm.PublishTime.ToString() + "";
}
/**//// <summary>
/// 建造Sql获取方法
/// </summary>
public override void BuilderGet()
{
base._operation["get"] = "SqlMessage|Get";
}
}
}
using System.Collections.Generic;
using System.Text;
namespace Pattern.Builder
{
/**//// <summary>
/// SqlMessage建造者(ConcreteBuilder)
/// </summary>
public class SqlMessageBuilder : AbstractMessageBuilder
{
/**//// <summary>
/// 构造函数
/// </summary>
public SqlMessageBuilder()
: base("sql")
{
}
/**//// <summary>
/// 建造Sql插入方法
/// </summary>
/// <param name="mm">Message实体对象</param>
public override void BuilderInsert(MessageModel mm)
{
base._operation["insert"] = "SqlMessage|Insert|" + mm.Message + "|" + mm.PublishTime.ToString() + "";
}
/**//// <summary>
/// 建造Sql获取方法
/// </summary>
public override void BuilderGet()
{
base._operation["get"] = "SqlMessage|Get";
}
}
}
XmlMessageBuilder
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.Builder
{
/**//// <summary>
/// XmlMessage建造者(ConcreteBuilder)
/// </summary>
public class XmlMessageBuilder : AbstractMessageBuilder
{
/**//// <summary>
/// 构造函数
/// </summary>
public XmlMessageBuilder()
: base("xml")
{
}
/**//// <summary>
/// 建造Xml插入方法
/// </summary>
/// <param name="mm">Message实体对象</param>
public override void BuilderInsert(MessageModel mm)
{
base._operation["insert"] = "XmlMessage|Insert|"+mm.Message+"|"+mm.PublishTime.ToString()+"";
}
/**//// <summary>
/// 建造Xml获取方法
/// </summary>
public override void BuilderGet()
{
base._operation["get"] = "XmlMessage|Get";
}
}
}
using System.Collections.Generic;
using System.Text;
namespace Pattern.Builder
{
/**//// <summary>
/// XmlMessage建造者(ConcreteBuilder)
/// </summary>
public class XmlMessageBuilder : AbstractMessageBuilder
{
/**//// <summary>
/// 构造函数
/// </summary>
public XmlMessageBuilder()
: base("xml")
{
}
/**//// <summary>
/// 建造Xml插入方法
/// </summary>
/// <param name="mm">Message实体对象</param>
public override void BuilderInsert(MessageModel mm)
{
base._operation["insert"] = "XmlMessage|Insert|"+mm.Message+"|"+mm.PublishTime.ToString()+"";
}
/**//// <summary>
/// 建造Xml获取方法
/// </summary>
public override void BuilderGet()
{
base._operation["get"] = "XmlMessage|Get";
}
}
}
Message
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.Builder
{
/**//// <summary>
/// 指导者(Director)
/// </summary>
public class Message
{
/**//// <summary>
/// 调用具体建造者角色以创建产品对象
/// </summary>
/// <param name="messageBuilder">AbstractMessageBuilder</param>
/// <param name="mm">Message实体对象</param>
public void Construct(AbstractMessageBuilder messageBuilder, MessageModel mm)
{
messageBuilder.BuilderInsert(mm);
messageBuilder.BuilderGet();
}
}
}
using System.Collections.Generic;
using System.Text;
namespace Pattern.Builder
{
/**//// <summary>
/// 指导者(Director)
/// </summary>
public class Message
{
/**//// <summary>
/// 调用具体建造者角色以创建产品对象
/// </summary>
/// <param name="messageBuilder">AbstractMessageBuilder</param>
/// <param name="mm">Message实体对象</param>
public void Construct(AbstractMessageBuilder messageBuilder, MessageModel mm)
{
messageBuilder.BuilderInsert(mm);
messageBuilder.BuilderGet();
}
}
}
client
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Pattern.Builder;
public partial class Builder : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Message m = new Message();
AbstractMessageBuilder amb = new SqlMessageBuilder();
m.Construct(amb, new MessageModel("插入", DateTime.Now));
Response.Write(amb.Operation.GetResult());
amb = new XmlMessageBuilder();
m.Construct(amb, new MessageModel("插入", DateTime.Now));
Response.Write(amb.Operation.GetResult());
}
}
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Pattern.Builder;
public partial class Builder : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Message m = new Message();
AbstractMessageBuilder amb = new SqlMessageBuilder();
m.Construct(amb, new MessageModel("插入", DateTime.Now));
Response.Write(amb.Operation.GetResult());
amb = new XmlMessageBuilder();
m.Construct(amb, new MessageModel("插入", DateTime.Now));
Response.Write(amb.Operation.GetResult());
}
}
运行结果
类型为sql的执行结果:
True
SQL方式获取Message 2007-5-5 20:53:28
类型为xml的执行结果:
True
XML方式获取Message 2007-5-5 20:53:28
参考
http://www.dofactory.com/Patterns/PatternBuilder.aspx
OK
[源码下载]