C#乐在其中设计情势(C#) – 享元格局(Flyweight 帕特tern)

[索引页] [源码下载]

乐在个中设计情势(C#) – 享元形式(Flyweight 帕特tern)

作者:webabcd

介绍
利用共享技术可行地协理大气细粒度的靶子。

示例
有3个Message实体类,某个对象对它的操作有Insert()和Get()方法,未来要运用共享技术帮助这几个目的。
C# 1

MessageModel

C# 2using System;
C# 3using System.Collections.Generic;
C# 4using System.Text;
C# 5
C# 6namespace Pattern.Flyweight
C# 7C# 8C# 9{
C# 10C# 11    /**//// <summary>
C# 12    /// Message实体类
C# 13    /// </summary>
C# 14    public class MessageModel
C# 15C# 16    C# 17{
C# 18C# 19        /**//// <summary>
C# 20        /// 构造函数
C# 21        /// </summary>
C# 22        /// <param name=”msg”>Message内容</param>
C# 23        /// <param name=”pt”>Message揭橥时间</param>
C# 24        public MessageModel(string msg, DateTime pt)
C# 25C# 26        C# 27{
C# 28            this._message = msg;
C# 29            this._publishTime = pt;
C# 30        }
C# 31
C# 32        private string _message;
C# 33C# 34        /**//// <summary>
C# 35        /// Message内容
C# 36        /// </summary>
C# 37        public string Message
C# 38C# 39        C# 40{
C# 41C# 42            get C# 43{ return _message; }
C# 44C# 45            set C# 46{ _message = value; }
C# 47        }
C# 48
C# 49        private DateTime _publishTime;
C# 50C# 51        /**//// <summary>
C# 52        /// Message发表时间
C# 53        /// </summary>
C# 54        public DateTime PublishTime
C# 55C# 56        C# 57{
C# 58C# 59            get C# 60{ return _publishTime; }
C# 61C# 62            set C# 63{ _publishTime = value; }
C# 64        }
C# 65    }
C# 66}
C# 67

AbstractMessage

C# 68using System;
C# 69using System.Collections.Generic;
C# 70using System.Text;
C# 71
C# 72namespace Pattern.Flyweight
C# 73C# 74C# 75{
C# 76C# 77    /**//// <summary>
C# 78    /// 操作Message抽象类(Flyweight)
C# 79    /// </summary>
C# 80    public abstract class AbstractMessage
C# 81C# 82    C# 83{
C# 84C# 85        /**//// <summary>
C# 86        /// 获取Message
C# 87        /// </summary>
C# 88        /// <returns></returns>
C# 89        public abstract List<MessageModel> Get();
C# 90
C# 91C# 92        /**//// <summary>
C# 93        /// 插入Message
C# 94        /// </summary>
C# 95        /// <param name=”mm”>Message实体对象</param>
C# 96        /// <returns></returns>
C# 97        public abstract bool Insert(MessageModel mm);
C# 98    }
C# 99}
C# 100

SqlMessage

C# 101using System;
C# 102using System.Collections.Generic;
C# 103using System.Text;
C# 104
C# 105namespace Pattern.Flyweight
C# 106C# 107C# 108{
C# 109C# 110    /**//// <summary>
C# 111    /// Sql情势操作Message(ConcreteFlyweight)
C# 112    /// </summary>
C# 113    public class SqlMessage : AbstractMessage
C# 114C# 115    C# 116{
C# 117C# 118        /**//// <summary>
C# 119        /// 获取Message
C# 120        /// </summary>
C# 121        /// <returns></returns>
C# 122        public override List<MessageModel> Get()
C# 123C# 124        C# 125{
C# 126            List<MessageModel> l = new List<MessageModel>();
C# 127            l.Add(new MessageModel(“SQL方式获取Message”, DateTime.Now));
C# 128
C# 129            return l;
C# 130        }
C# 131
C# 132C# 133        /**//// <summary>
C# 134        /// 插入Message
C# 135        /// </summary>
C# 136        /// <param name=”mm”>Message实体对象</param>
C# 137        /// <returns></returns>
C# 138        public override bool Insert(MessageModel mm)
C# 139C# 140        C# 141{
C# 142            // 代码略
C# 143            return true;
C# 144        }
C# 145    }
C# 146}
C# 147

XmlMessage

C# 148using System;
C# 149using System.Collections.Generic;
C# 150using System.Text;
C# 151
C# 152namespace Pattern.Flyweight
C# 153C# 154C# 155{
C# 156C# 157    /**//// <summary>
C# 158    /// Xml格局操作Message(ConcreteFlyweight)
C# 159    /// </summary>
C# 160    public class XmlMessage : AbstractMessage
C# 161C# 162    C# 163{
C# 164C# 165        /**//// <summary>
C# 166        /// 获取Message
C# 167        /// </summary>
C# 168        /// <returns></returns>
C# 169        public override List<MessageModel> Get()
C# 170C# 171        C# 172{
C# 173            List<MessageModel> l = new List<MessageModel>();
C# 174            l.Add(new MessageModel(“XML情势赢得Message”, DateTime.Now));
C# 175
C# 176            return l;
C# 177        }
C# 178
C# 179C# 180        /**//// <summary>
C# 181        /// 插入Message
C# 182        /// </summary>
C# 183        /// <param name=”mm”>Message实体对象</param>
C# 184        /// <returns></returns>
C# 185        public override bool Insert(MessageModel mm)
C# 186C# 187        C# 188{
C# 189            // 代码略
C# 190            return true;
C# 191        }
C# 192    }
C# 193}
C# 194

MessageFactory

C# 195using System;
C# 196using System.Collections.Generic;
C# 197using System.Text;
C# 198
C# 199namespace Pattern.Flyweight
C# 200C# 201C# 202{
C# 203C# 204    /**//// <summary>
C# 205    /// Message工厂(FlyweightFactory)
C# 206    /// </summary>
C# 207    public class MessageFactory
C# 208C# 209    C# 210{
C# 211        private Dictionary<string, AbstractMessage> _messageObjects = new Dictionary<string, AbstractMessage>();
C# 212
C# 213C# 214        /**//// <summary>
C# 215        /// 获取Message对象
C# 216        /// </summary>
C# 217        /// <param name=”key”>key</param>
C# 218        /// <returns></returns>
C# 219        public AbstractMessage GetMessageObject(string key)
C# 220C# 221        C# 222{
C# 223            AbstractMessage messageObject = null;
C# 224
C# 225            if (_messageObjects.ContainsKey(key))
C# 226C# 227            C# 228{
C# 229                messageObject = _messageObjects[key];
C# 230            }
C# 231            else
C# 232C# 233            C# 234{
C# 235                switch (key)
C# 236C# 237                C# 238{
C# 239                    case “xml”: messageObject = new SqlMessage(); break;
C# 240                    case “sql”: messageObject = new XmlMessage(); break;
C# 241                }
C# 242
C# 243                _messageObjects.Add(key, messageObject);
C# 244            }
C# 245
C# 246            return messageObject;
C# 247        }
C# 248    }
C# 249}
C# 250

Test

C# 251using System;
C# 252using System.Data;
C# 253using System.Configuration;
C# 254using System.Collections;
C# 255using System.Web;
C# 256using System.Web.Security;
C# 257using System.Web.UI;
C# 258using System.Web.UI.WebControls;
C# 259using System.Web.UI.WebControls.WebParts;
C# 260using System.Web.UI.HtmlControls;
C# 261
C# 262using Pattern.Flyweight;
C# 263
C# 264public partial class Flyweight : System.Web.UI.Page
C# 265C# 266C# 267{
C# 268    protected void Page_Load(object sender, EventArgs e)
C# 269C# 270    C# 271{
C# 272C# 273        string[] ary = new string[] C# 274{ “xml”, “sql” };
C# 275
C# 276        MessageFactory messageFactory = new MessageFactory();
C# 277
C# 278        foreach (string key in ary)
C# 279C# 280        C# 281{
C# 282            AbstractMessage messageObject = messageFactory.GetMessageObject(key);
C# 283
C# 284            Response.Write(messageObject.Insert(new MessageModel(“插入”, DateTime.Now)));
C# 285            Response.Write(“<br />”);
C# 286            Response.Write(messageObject.Get()[0].Message + ” ” + messageObject.Get()[0].PublishTime.ToString());
C# 287            Response.Write(“<br />”);
C# 288        }
C# 289    }
C# 290}
C# 291

运营结果
True
SQL格局获得Message 二〇〇五-5-17 22:20:38
True
XML格局赢得Message 二〇〇五-5-17 22:20:38

参考
http://www.dofactory.com/Patterns/PatternFlyweight.aspx

OK
[源码下载]

相关文章