建造者模式(Builder Pattern)
介绍
将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。
示例
用同样的构建过程创建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; }
}
}
}
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;
}
}
}
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;
}
}
}
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 = assembly.GetType("Pattern.Builder." + _dictionary["get"].Split('|')[0]).GetMethod(_dictionary["get"].Split('|')[1]);
object objGet = methodGet.Invoke(assembly.CreateInstance("Pattern.Builder." + _dictionary["get"].Split('|')[0]), null);
List<MessageModel> m = (List<MessageModel>)objGet; MethodInfo methodInsert = assembly.GetType("Pattern.Builder." + _dictionary["insert"].Split('|')[0]).GetMethod(_dictionary["insert"].Split('|')[1]);
object objInsert = methodInsert.Invoke(assembly.CreateInstance("Pattern.Builder." + _dictionary["insert"].Split('|')[0]), new object[] { new MessageModel(_dictionary["insert"].Split('|')[2], Convert.ToDateTime(_dictionary["insert"].Split('|')[3])) });
bool b = (bool)objInsert; return "类型为" + this._type + "的执行结果:<br />" + b.ToString() + "<br />" + m[0].Message + " " + m[0].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();
}
}
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";
}
}
}
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";
}
}
}
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();
}
}
}
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());
}
}
运行结果
类型为sql的执行结果:
True
SQL方式获取Message 2007-5-5 20:53:28
类型为xml的执行结果:
True
XML方式获取Message 2007-5-5 20:53:28