首页 > 代码库 > 二十四种设计模式:享元模式(Flyweight Pattern)

二十四种设计模式:享元模式(Flyweight Pattern)

享元模式(Flyweight Pattern)


介绍
运用共享技术有效地支持大量细粒度的对象。


示例
有一个Message实体类,某些对象对它的操作有Insert()和Get()方法,现在要运用共享技术支持这些对象。

技术分享

  MessageModel

using System;
using System.Collections.Generic;
using System.Text;

namespace Pattern.Flyweight
{
    /// <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; }
        }
    }
}

  AbstractMessage

using System;
using System.Collections.Generic;
using System.Text;

namespace Pattern.Flyweight
{
    /// <summary>
    /// 操作Message抽象类(Flyweight)
    /// </summary>
    public abstract class AbstractMessage
    {
        /// <summary>
        /// 获取Message
        /// </summary>
        /// <returns></returns>
        public abstract List<MessageModel> Get();

        /// <summary>
        /// 插入Message
        /// </summary>
        /// <param name="mm">Message实体对象</param>
        /// <returns></returns>
        public abstract bool Insert(MessageModel mm);
    }
}

  SqlMessage

using System;
using System.Collections.Generic;
using System.Text;

namespace Pattern.Flyweight
{
    /// <summary>
    /// Sql方式操作Message(ConcreteFlyweight)
    /// </summary>
    public class SqlMessage : AbstractMessage
    {
        /// <summary>
        /// 获取Message
        /// </summary>
        /// <returns></returns>
        public override 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 override bool Insert(MessageModel mm)
        {
            // 代码略
            return true;
        }
    }
}

  XmlMessage

using System;
using System.Collections.Generic;
using System.Text;

namespace Pattern.Flyweight
{
    /// <summary>
    /// Xml方式操作Message(ConcreteFlyweight)
    /// </summary>
    public class XmlMessage : AbstractMessage
    {
        /// <summary>
        /// 获取Message
        /// </summary>
        /// <returns></returns>
        public override 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 override bool Insert(MessageModel mm)
        {
            // 代码略
            return true;
        }
    }
}

  MessageFactory

using System;
using System.Collections.Generic;
using System.Text;

namespace Pattern.Flyweight
{
    /// <summary>
    /// Message工厂(FlyweightFactory)
    /// </summary>
    public class MessageFactory
    {
        private Dictionary<string, AbstractMessage> _messageObjects = new Dictionary<string, AbstractMessage>();

        /// <summary>
        /// 获取Message对象
        /// </summary>
        /// <param name="key">key</param>
        /// <returns></returns>
        public AbstractMessage GetMessageObject(string key)
        {
            AbstractMessage messageObject = null;

            if (_messageObjects.ContainsKey(key))
            {
                messageObject = _messageObjects[key];
            }
            else
            {
                switch (key)
                {
                    case "xml": messageObject = new SqlMessage(); break;
                    case "sql": messageObject = new XmlMessage(); break;
                }

                _messageObjects.Add(key, messageObject);
            }

            return messageObject;
        }
    }
}

  Test

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.Flyweight;

public partial class Flyweight : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string[] ary = new string[] { "xml", "sql" };

        MessageFactory messageFactory = new MessageFactory();

        foreach (string key in ary)
        {
            AbstractMessage messageObject = messageFactory.GetMessageObject(key);

            Response.Write(messageObject.Insert(new MessageModel("插入", DateTime.Now)));
            Response.Write("<br />");
            Response.Write(messageObject.Get()[0].Message + " " + messageObject.Get()[0].PublishTime.ToString());
            Response.Write("<br />");
        }
    }
}

  运行结果
  True
  SQL方式获取Message 2007-5-17 22:20:38
  True
  XML方式获取Message 2007-5-17 22:20:38

二十四种设计模式:享元模式(Flyweight Pattern)