24 Design modes: Enjoy meta mode (Flyweight pattern)

Source: Internet
Author: User

Enjoy meta mode (Flyweight pattern)


Introduced
Use shared technology to effectively support a large number of fine-grained objects.


Example
There is a message entity class, and some objects have an insert () and get () method for its operations, and these objects are now supported using shared technology.

  Messagemodel

Using system;using system.collections.generic;using system.text;namespace pattern.flyweight{//<summary>// Message entity class///</summary> public class Messagemodel {//<summary>///constructor// /</summary>//<param name= "msg" >message content </param>//<param name= "PT" >message when released            Room </param> public Messagemodel (String msg, DateTime pt) {this._message = msg;        This._publishtime = pt;        } private string _message; <summary>//message content///</summary> public string Message {get            {return _message;}        set {_message = value;}        } private DateTime _publishtime;            <summary>//Message release time///</summary> public DateTime Publishtime {            get {return _publishtime;}        set {_publishtime = value;}    }}} 

  Abstractmessage

Using system;using system.collections.generic;using system.text;namespace pattern.flyweight{//    <summary >    ///Operation Message abstract class (Flyweight)        ///</summary> public abstract class Abstractmessage {/ <summary>///Get Message///</summary>//        <returns></returns>        Public Abstract list<messagemodel> Get ();        <summary>///INSERT message///</summary>//        <param name= "MM" >message entity object </ param>        //<returns></returns> public abstract bool Insert (Messagemodel mm);}    }

  Sqlmessage

Using system;using system.collections.generic;using system.text;namespace pattern.flyweight{//    <summary >    ///SQL Operation message (concreteflyweight)///    </summary> Public    class Sqlmessage: Abstractmessage {//<summary>///        Get message//</summary>//        <returns> </returns> public        override list<messagemodel> Get ()        {            list<messagemodel> l = new List <MessageModel> ();            L.add (New Messagemodel ("SQL Get Message", DateTime.Now));            return l;        }        <summary>///INSERT message///</summary>//        <param name= "MM" >message entity object </ param>        //<returns></returns> public override bool Insert (Messagemodel mm)        {            // The code is slightly            return true;}}    }

  Xmlmessage

Using system;using system.collections.generic;using system.text;namespace pattern.flyweight{//    <summary >    ///XML operation message (Concreteflyweight)///    </summary> Public    class Xmlmessage: Abstractmessage {//<summary>///        Get message//</summary>//        <returns> </returns> public        override list<messagemodel> Get ()        {            list<messagemodel> l = new List <MessageModel> ();            L.add (New Messagemodel ("XML Get Message", DateTime.Now));            return l;        }        <summary>///INSERT message///</summary>//        <param name= "MM" >message entity object </ param>        //<returns></returns> public override bool Insert (Messagemodel mm)        {            // The code is slightly            return true;}}    }

  Messagefactory

Using system;using system.collections.generic;using system.text;namespace pattern.flyweight{//<summary>// Message Factory (Flyweightfactory)///</summary> public class Messagefactory {private Dictionary<stri        Ng, abstractmessage> _messageobjects = new dictionary<string, abstractmessage> (); <summary>///Get Message object///</summary>//<param name= "key" >key</param&gt        ; <returns></returns> Public abstractmessage Getmessageobject (string key) {Abstrac            Tmessage 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 = n        EW string[] {"xml", "SQL"};        Messagefactory messagefactory = new Messagefactory ();            foreach (string key in ary) {abstractmessage messageobject = Messagefactory.getmessageobject (key);            Response.Write (Messageobject.insert (New Messagemodel ("Insert", DateTime.Now));            Response.Write ("<br/>"); Response.Write (Messageobject.get () [0]. Message + "" + messageobject.get () [0].            Publishtime.tostring ());        Response.Write ("<br/>"); }    }}

  Run results
True
SQL Get message 2007-5-17 22:20:38
True
XML way to get message 2007-5-17 22:20:38

24 Design modes: Enjoy meta mode (Flyweight pattern)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.