First, the definition
Enjoy meta-mode: Use sharing technology to effectively support a large number of fine-grained objects.
Explanation: When you need to repeat a large number of new objects, using the enjoy meta-mode allows you to reduce the initialization of objects, thereby reducing memory overhead. Too pale, understanding is not in place, I hope to add it later.
Ii. UML class diagram and Basic code
Basic code:
Public Abstract classFlyweight { Public Abstract voidOperation (intextrinsicstate); } Public classConcreteflyweight:flyweight {//Internal State Private stringintrinsicstate; //constructor Function PublicConcreteflyweight (stringinnerstate) { This. intrinsicstate =innerstate; } /// <summary> ///An instance method of the class of enjoyment/// </summary> /// <param name= "Extrinsicstate" >External State</param> Public Override voidOperation (intextrinsicstate) {Console.WriteLine ("concrete Implementation class: Intrinsicstate {0}, extrinsicstate {1}", Intrinsicstate, extrinsicstate); } } Public classFlyweightfactory {//It is best to use the generic dictionary<string,flyweighy>//Public dictionary<string, flyweight> flyweights = new dictionary<string, flyweight> (); PublicHashtable flyweights =NewHashtable (); Publicflyweightfactory () {flyweights. ADD ("A",NewConcreteflyweight ("A")); Flyweights. ADD ("B",NewConcreteflyweight ("B")); Flyweights. ADD ("C",NewConcreteflyweight ("C")); } PublicFlyweight Getflyweight (stringkey) { //better to achieve the following//Flyweight Flyweight = Flyweights[key] as Flyweight; //if (flyweight = = null)//{ //Console.WriteLine ("String not present in the resident pool" + key); //flyweight = new Concreteflyweight (key); //} //return flyweight; returnFlyweights[key] asFlyweight; } }
Client calls:
classProgram {Static voidMain (string[] args) { //define external states, such as the location of letters intExternalstate =Ten; //initialize the enjoy meta factoryFlyweightfactory factory =Newflyweightfactory (); //determine if the letter A has been created, and if it has been created, use the object created directlyFlyweight FA = Factory. Getflyweight ("A"); if(FA! =NULL) { //to call parameters for method invocation of an external state as a privilege objectFa. Operation (--externalstate); } //determine if the letter B has been createdFlyweight fb = Factory. Getflyweight ("B"); if(FB! =NULL) {fb. Operation (--externalstate); } //determine if the letter D has been createdFlyweight FD = Factory. Getflyweight ("D"); if(FD! =NULL) {fd. Operation (--externalstate); } Else{Console.WriteLine ("string d does not exist in the resident pool"); //At this point , you need to create an object and put it in the resident poolConcreteflyweight d =NewConcreteflyweight ("D"); FACTORY.FLYWEIGHTS.ADD ("D", D); } console.read (); } }
View Code
Operation Result:
Third, specific examples:
Build a common web site that is used by different users, with the following code:
Public classUser {Private stringname; Public stringName {Get{returnname;} } PublicUser (stringname) { This. Name =name; } } Abstract classWebsite { Public Abstract voidUse (user user); } classConcretewebsite:website {Private stringName =""; PublicConcretewebsite (stringname) { This. Name =name; } Public Override voidUse (user user) {Console.WriteLine ("website Type:"+ name +"User:"+user. Name); } } classWebsitefactory {PrivateHashtable flyweights =NewHashtable (); PublicWebsite Getwebsitecategory (stringkey) { if(!flyweights. ContainsKey (key)) {flyweights. ADD (Key,NewConcretewebsite (key)); } return((Website) flyweights[key]); } Public intGetwebsitecount () {returnflyweights. Count; } }
Client calls:
Websitefactory WF =Newwebsitefactory (); Website WX= WF. Getwebsitecategory ("Show"); Wx. Use (NewUser ("a")); Website WY= WF. Getwebsitecategory ("Show"); Wy. Use (NewUser ("b")); Website wz= WF. Getwebsitecategory (" See"); Wz. Use (NewUser ("C")); Console.WriteLine ("website Type num:"+ WF. Getwebsitecount ());
View Code
Iv. advantages and disadvantages and application scenarios
Advantages:
Reduces the number of objects in the system, thus reducing the pressure on memory caused by fine-grained objects in the system.
Disadvantages:
1) in order for objects to be shared, some of the state of the object needs to be externally instantiated, increasing the complexity of the system
2) The state of the object is externally instantiated, making the read external state run slightly longer.
Applicable scenarios:
A large number of fine-grained objects are required in the system, and these objects consume a lot of memory, and the state of the objects can be largely externally, and it is not wrong to use the enjoy meta-mode.
Design mode (---) enjoy meta mode