12. Enjoy meta mode (Flyweight pattern)

Source: Internet
Author: User

usingSystem;usingSystem.Collections;namespaceconsoleapplication5{classProgram {/// <summary>        ///in the software development process, if we need to reuse an object,///If we repeatedly use new to create this object, then we need to apply memory space multiple times in memory,///This can occur in more and more memory usage, and the problem is very serious, but the meta-mode can solve this problem,///Here's a look at how the enjoy meta-mode solves this problem. /// </summary>        /// <param name= "args" ></param>        Static voidMain (string[] args) {Charactorfactory Factory=Newcharactorfactory (); //charactor "A"Charactora CA = (Charactora) factory. Getcharactor ("A"); Ca. Setpointsize ( A); Ca.            Display (); //charactor "B"Charactorb cb = (Charactorb) factory. Getcharactor ("B"); Cb. Setpointsize (Ten); Cb.            Display (); //charactor "C"Charactorc cc = (CHARACTORC) factory. Getcharactor ("C"); Cc. Setpointsize ( -); Cc.        Display (); }    }    //Character factory (responsible for creating and managing the enjoy meta object)     Public classCharactorfactory {// Fields        PrivateHashtable charactors =NewHashtable (); //Constructor         Publiccharactorfactory () {charactors. ADD ("A",NewCharactora ()); Charactors. ADD ("B",NewCharactorb ()); Charactors. ADD ("C",NewCharactorc ()); }        //Method         PublicCharactor Getcharactor (stringkey) {charactor charactor= Charactors[key] ascharactor; if(Charactor = =NULL)            {                Switch(key) { Case "A": Charactor =NewCharactora (); Break;  Case "B": Charactor =NewCharactorb (); Break;  Case "C": Charactor =NewCharactorc (); Break; //} charactors.            ADD (key, charactor); }            returncharactor; }    }    /// <summary>    ///A special class of characters that provides a method for a specific class of enjoyment/// </summary>     Public Abstract classCharactor {// Fields        protected Char_symbol; protected int_width; protected int_height; protected int_ascent; protected int_descent; protected int_pointsize; //Method         Public Abstract voidSetpointsize (intsize);  Public Abstract voidDisplay (); }    /// <summary>    ///character a/// </summary>     Public classCharactora:charactor {//Constructor         PublicCharactora () { This. _symbol ='A';  This. _height = -;  This. _width = -;  This. _ascent = -;  This. _descent =0;  This. _pointsize = A; }        //Method         Public Override voidSetpointsize (intsize) {             This. _pointsize =size; }         Public Override voidDisplay () {Console.WriteLine ( This. _symbol +"pointsize:"+ This. _pointsize); }    }    /// <summary>    ///Character B/// </summary>     Public classCharactorb:charactor {//Constructor         PublicCharactorb () { This. _symbol ='B';  This. _height = -;  This. _width = $;  This. _ascent = the;  This. _descent =0;  This. _pointsize =Ten; }        //Method         Public Override voidSetpointsize (intsize) {             This. _pointsize =size; }         Public Override voidDisplay () {Console.WriteLine ( This. _symbol +"pointsize:"+ This. _pointsize); }    }    /// <summary>    ///character C/// </summary>     Public classCharactorc:charactor {//Constructor         PublicCharactorc () { This. _symbol ='C';  This. _height = -;  This. _width = the;  This. _ascent = About;  This. _descent =0;  This. _pointsize = -; }        //Method         Public Override voidSetpointsize (intsize) {             This. _pointsize =size; }         Public Override voidDisplay () {Console.WriteLine ( This. _symbol +"pointsize:"+ This. _pointsize); }    }}

12. 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.