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? Share mode: Use the sharing technology to effectively support a large number of fine-grained object structure charts: explanation: FlyweightFactory: A share factory is used to create and manage Flyweight objects. It is mainly used to ensure reasonable share of Flyweight, when a user requests a Flyweight, The FlyweightFactory object provides a created instance or creates a (if it does not exist) Flyweight: The superclass or interface of all the specific Metadata classes. Through this interface, flyweight permission accepts and acts on the external State ConcreteFlyweight: inherits Flyweight over-tired or implements the Flyweight interface, and adds the storage space UnsharedConcreteFlyweight to the internal state: the Flyweight subclass that does not need to be shared, because the Flyweight interface is shared, it does not force the code to be shared: [html] <span style = "font-size: 18px;"> using Sy Stem; 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 the Flyweight superclass or implement the Flyweight interface, and add the storage space class ConcreteFlyweight: Flyweight {www.2cto.com public override void O for the internal status Peration (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. collect Ions; 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]);} </span> example: website sharing structure: code implementation: [html] <span style = "font-size: 18px;"> using System; using System. C Ollections. 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;} p Ublic string Name {get {return name ;}}// WebSite abstract class WebSite {public abstract void Use (User user User) ;}// the 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 ;}}</span> running result: if an application in the metadata mode uses a large number of objects, A large number of these objects cause a large storage overhead when most of the object's states can be external states, if you delete the object's external state, therefore, you can replace the multi-group object metadata mode with a relatively small number of shared objects. The more shared objects, the more storage savings, the savings increase 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.