[design mode] JavaScript to enjoy the meta-mode;

Source: Internet
Author: User

Enjoy meta mode description

Definition: A shared problem used to solve a large number of fine-grained objects in a system:

Key words: separation and sharing;

Description: Enjoy meta-mode divided into simple (shared) Enjoy meta-mode, and combined (not shared) to enjoy the meta-mode, share and not share the sub-mode, simply enjoy the meta-model, only the shared state, the shared state is immutable, non-modifiable, this is the internal state of the enjoyment of the element, of course, the external state is is not shared, this external state is managed by the client, is changeable, the external state is separate from the internal state, and the external state is generally passed in as a parameter to the value of the object, but does not affect the values of the internal state; , such as fetching data from the user data table, passing in an external variable username, and then obtaining all the object state information of the user according to the user name;

The combination of the meta-mode, the configuration of the combined mode to use, the shared part is only a leaf node, and the combined part is not necessary to share, because the total subset of the sub-leaf node elements have been cached, the composition of the group is quite a grouping of the role of classification;

The shared meta-mode is typically used in conjunction with the factory model to manage the creation and maintenance of the cache, and the creation of the sharing element is typically created in the factory class;

Enjoy meta-mode is mainly used for the sharing of objects, so that have similar objects, or objects have too many similar fine-grained state, can share the objects that have been created, reduce the creation of objects, reduce the use of memory, improve performance optimization;

A scenario:

Enjoy meta-mode structure diagram:

Like the first case, you can use the enjoy meta-mode, because the second two rows of the state combination, may be in the entity corresponding, multiple times, then you can use the sharing features of the share, to reduce the creation of objects, such as the same state or combination, you can get the state of the cache, reduce the object duplication creation, reduce memory consumption;

Instance scenario:

1>. System various role permission assignment;

2>. The system appears many kinds of states, or the combination state needs corresponding, and this correspondence, can occur very many other types of objects above, you can use the enjoy meta-mode;

Example Source: pure (shared) to enjoy the yuan

1. Custom JavaScript Hashtable:

functionHashtable () { This. Values = [];} Hashtable.prototype.getJson=function() {return  This. Values;} Hashtable.prototype.add=function(key, value) {if(! This. Contain (key)) {         This. Values.push ({key:key, value:value}); }}hashtable.prototype.contain=function(key) { for(IDXinch  This. Values) {        varJSON = This. Values[idx]; if(Json.key = =key) {            return true; }    }    return false;} Hashtable.prototype.get=function(key) {varresult;  for(IDXinch  This. Values) {        varJSON = This. Values[idx]; if(Json.key = =key) {Result=JSON;  Break; }    }    returnresult;} Hashtable.prototype.Delete=function(key) { for(IDXinch  This. Values) {        varJSON = This. Values[idx]; if(Json.key = =key) {            Delete  This. Values[idx];  Break; }}}hashtable.prototype.set=function(key, value) {if( This. Contain (key)) {         This.Delete(key);  This. Values.push ({key:key,value:value}); }}

2. Enjoy the meta-method:

function Flyweight (one) {    this. Stateone = one ;   function() {
var New Date (); Console.log (this. Stateone + '-' + math.random ());};

Here you can handle some of the arguments passed in the logic, you can also initialize the data from the database to extract the cache to save;

3. Enjoy the meta-factory:

 function   Flyweightfactory () { Span style= "color: #0000ff;" >var  hash = new   Hashtable ();} FlyweightFactory.prototype.getFlyweight  = function   var   flyweight;  if   (Hash.contain (state)) {Flyweight = Hash.get (state);  else   {flyweight  = new   Flyweight (state);    Hash.add (state, flyweight);  return   flyweight;}  

4. Client use:

var new  flyweightfactory ();    var  = factory.getflyweight (' AABB '= factory.getflyweight (' CCDD '= factory.getflyweight (' CCDD '); Fly1.operate (); Fly2.operate (); Fly3.operate () ;

Output:

AABB-0.8621504916809499

CCDD-0.7498800195753574

CCDD-0.7498800195753574

Compound enjoy meta mode

1. Compound to enjoy the yuan

functionunshareflyweight () { This. State = ' ';  This. hash =new Hashtable ();} UnShareFlyweight.prototype.add=function(key, flyweight) { This. Hash.add (key, flyweight);} UnShareFlyweight.prototype.operate=function(state) {varFlag =false;
   /* For (idx in this.hash) {var fly = This.list[idx]; if (Fly.stateone = = state) {flag = True;
Break } }
*/
  
flag = This.hash.contain (state); Flag? Console.log (' Exists '): Console.log (' not Exists '));}

5. In the modified combination method added at the Mövenpick Factory:

functionflyweightfactory () {varhash =NewHashtable ();} FlyweightFactory.prototype.getFlyweight=function(state) {varflyweight; if(Hash.contain (state)) {Flyweight=Hash.get (state); } Else{Flyweight=NewFlyweight (state);    Hash.add (state, flyweight); }    returnflyweight;} FlyweightFactory.prototype.getComposite=function(list) {varUnfly =Newunshareflyweight (); varflyweight, state;  for(IDXinchlist) { state=List[idx]; Flyweight= This. Getflyweight (state);    Unfly.add (state, flyweight); }}flyweightfactory.prototype.print=function() {    varJsons = This. Hash.getjson ();  for(JSONinchjsons)    {json.value.operate (); }}

3. Client use:

var states = [' AABB ', ' cdcd ', ' AABB ', ' CCDD ']; var New flyweightfactory (); Factory.getcomposite (states); Factory.print ();

Output:

AABB-0.8749617566354573

CDCD-0.6991151459515095

CCDD-0.9891050879377872

Other notes on the enjoy meta mode

In general, the enjoy meta-mode is used to reduce the duplication of object creation, which is used to reduce memory usage and improve performance in a structured pattern:

It involves three modes: Enjoy meta-mode, Factory mode, combined mode;

It's a good choice for some data cache storage to handle many-to-many correspondence!

[design mode] JavaScript to enjoy the meta-mode;

Related Article

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.