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)