In the actual project development process, we must have encountered the following similar situation: For small companies to register and display the company information, to provide different styles of display; we do not develop a tailor-made website for each registered account company, and use shared technology to implement core technologies, Other changes are introduced in the way of external variables. This is what we are going to talk about today in the meta mode.
What is the enjoy meta mode? Use shared technology to effectively support a large number of fine-grained objects. In other words, you reduce the number of objects you create and reduce your system overhead. Because our hardware resources are limited;
Code section:
The flyweight class is the superclass, which is the interface of the specific class, and the external state is passed in and can be processed;
Abstract class Flyweight{public abstract void operation (int extrinsicstate);}
The Concreteflyweight class is a specific class of enjoyment;
Class concreteflyweight:flyweight{public override void operation (int extrinsicstate) {Console.WriteLine ("Specific enjoy Meta object:" + extrinsicstate);}}
We must set aside the situation of non-enjoyment of the yuan, to facilitate other operations;
Class unshareconcreteflyweight:flyweight{public override void operation (int extrinsicstate) {Console.WriteLine (" Do not share the specific object of the privilege: "+extrinsicstate);}}
We need to use a factory class to manage our own object, to get it, to create it if it does not exist, to reduce the number of objects to be used, and to reduce the cost of storage;
Class Flyweightfactory{private Hashtable flyweights=new Hashtable ();p ublic flyweightfactory () { //Initialize the object of the element, We can also do this without initialization, and if it is null when fetching the data, add Flyweights is created automatically. ADD ("x", New Concreteflyweight ()); flyweights. ADD ("Y", New Concreteflyweight ()); flyweights. ADD ("Z", New Concreteflyweight ());} Public Flyweight Getflyweight (string key) { //If we are not initializing the object, there is no way to add/*if (!flyweights. ContainsKey (key)) {flyweights. ADD (Key,new concreteflyweight ());} */return ((Flyweight) flyweights[key]);}}
The client is relatively simple, just be aware that we have an external state in this place. We actually distinguish between different display modes according to the external variables. For example, when registering and displaying information from different companies, we can pass in the information displayed by our system, based on the ID (unique value, external status) of the application, and display it according to the style format set by the ID.
Client static void Main (string[] arg) { //Code external State, this is the change part int extrinsicstate = 100; Flyweightfactory fly=new flyweightfactory (); Flyweight Fx=fly. Getflyweight ("X"); FX. Operation (extrinsicstate--); Flyweight Fx=fly. Getflyweight ("Y"); FX. Operation (extrinsicstate--); Flyweight Fx=fly. Getflyweight ("Z"); FX. Operation (extrinsicstate--); Unshareconcreteflyweight ux=new unshareconcreteflyweight (); UX. Operation (extrinsicstate--);}
Develop multi-user registration using the site---enjoy meta mode