Sharing the metadata of the big talk Design Model

Source: Internet
Author: User

The object-oriented thinking solves the abstract problem well, and generally does not have performance problems. However, in some cases, the number of objects may be too large, resulting in the runtime cost. So how can we avoid a large number of fine-grained objects without affecting the use of object-oriented operations by customer programs?

 

Metadata mode:Use sharing technology to effectively support a large number of fine-grained objects

 

Structure:



Explanation:

 

  • Flyweightfactory: A metadata factory used to create and manage flyweight objects. It is mainly used to ensure reasonable sharing of Flyweight. When a user requests a flyweight, the flyweightfactory object provides a created instance or creates one (if it does not exist)

 

  • Flyweight: The superclass or interface of all the specific Metadata classes. Through this interface, flyweight grants accepts and acts on the external state.

 

  • Concreteflyweight: inherits flyweight over-tired or implements the flyweight interface, and adds storage space for internal states

 

  • Unsharedconcreteflyweight: refers to the flyweight subclasses that do not need to be shared. Because the flyweight interface is shared, it is not forced to share.


Code implementation:


Using system; using system. collections. generic; using system. LINQ; using system. text; using system. collections; namespace metadata mode {class program {static void main (string [] ARGs) {// code external State int extrinsicstate = 22; flyweightfactory F = new flyweightfactory (); flyweight FX = f. getflyweight ("X"); FX. operation (-- extrinsicstate); flyweight FY = f. getflyweight ("Y"); FY. operation (-- extrinsicstate); flyweight FZ = f. getflyweight ("Z"); FZ. operation (-- extrinsicstate); flyweight UF = new unsharedconcreteflyweight (); UF. operation (-- extrinsicstate); console. read () ;}/// flyweight class, which is the superclass or interface of all the specific Metadata classes abstract class flyweight {public abstract void operation (INT extrinsicstate);} // concreteflyweight class, inherit from the flyweight superclass or implement the flyweight interface, and add the bucket class concreteflyweight: flyweight {public override void operation (INT extrinstate) {console. writeline ("Specific flyweight:" + extrinstate) ;}// category class, representing the flyweight subclass class unsharedconcreteflyweight: flyweight {public override void operation (INT extrinsicstate) that does not need to be shared) {console. writeline ("do not share the specific flyweight:" + extrinsicstate) ;}}// flyweightfactory class, is a metadata factory, used to create and manage the flyweight object class flyweightfactory {// to use hashtable, you need to call using system. collections; private hashtable flyweights = new hashtable (); Public flyweightfactory () {flyweights. add ("X", new concreteflyweight (); flyweights. add ("Y", new concreteflyweight (); flyweights. add ("Z", new concreteflyweight ();} public flyweight getflyweight (string key) {return (flyweight) flyweights [Key]);}


Example: website sharing


Structure:



Code implementation:

Using system; using system. collections. generic; using system. LINQ; using system. text; using system. collections; namespace website sharing code {class program {static void main (string [] ARGs) {websitefactory F = new websitefactory (); website FX = f. getwebsitecategory ("product presentation"); FX. use (new user (""); website FY = f. getwebsitecategory ("product display"); FY. use (new user ("Chen Jinrong"); website FZ = f. getwebsitecategory ("product presentation"); FZ. Use (new user ("Han xuemin"); website FL = f. getwebsitecategory ("blog"); FL. use (new user (""); website fm = f. getwebsitecategory ("blog"); FM. use (new user ("Zhou zhiqing"); website fn = f. getwebsitecategory ("blog"); fn. use (new user ("Tang Huan"); console. writeline ("the total number of website categories obtained is {0}", F. getwebsitecount (); console. read () ;}// user class, used for the Customer Account of the website. It is the external State of the "Website" class public class user {private string name; public user (string name ){ This. name = Name ;}public string name {get {return name ;}}// abstract class abstract website {public abstract void use (User user );} // specific website class concretewebsite: website {private string name = ""; Public concretewebsite (string name) {This. name = Name;} public override void use (User user) {console. writeline ("website category:" + name + "User:" + User. name) ;}}// class websitefactory {private Hashtable flyweights = new hashtable (); // obtain the website category public website getwebsitecategory (string key) {If (! Flyweights. containskey (key) flyweights. add (Key, new concretewebsite (key); Return (website) flyweights [Key]);} // obtain the total number of website categories public int getwebsitecount () {return flyweights. count ;}}}


Running result:




Application of the metadata-sharing model

 

  • If an application uses a large number of objects, and a large number of these objects cause a large storage overhead
  • When most states of objects can be external states, if you delete the external States of objects, you can replace multiple groups of objects with a relatively small number of shared objects.

 

 

Advantages of the meta-mode:

 

  • Fewer instances
  • The more shared objects, the more storage savings, and the larger the savings as the sharing status increases

 

 




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.