The style of vegetable design--the mode of enjoying meta

Source: Internet
Author: User


background

If a large number of objects are used in an application, and a large number of these objects cause the hated storage overhead, this new design pattern should be considered: the enjoy meta-mode.


1. Intention to use

Minimizes the use of data-sharing memory as much as possible with other similar objects, in other words, reduces system overhead by reducing the number of objects maintained within the system through shared objects. Because it is fine-grained control, so the use of meta-mode is not to control the entire instance sharing, then why not a single case? Instead of just sharing the part of the object, the part that is not shared is called the external state of the object, and the shared object needs to be dynamically modified externally.



2. Life Example

Go chess pieces, if programmed to achieve, it is bound to new out many instances, the whole game at the end of the estimated memory is also fast explosion. If you use the enjoy meta-mode, the common characteristics of go out, some external state is the external dynamic control, then this efficiency is optimal. For example, the share should be the shape of this piece, size, picture, external dynamic modification should be the position of the pieces. So, just define two classes, black and white, and the factory or the whole system will only maintain two objects, one is black chess and white.



3. Java Example (framework, JDK, JEE)

The maintenance of Java string is the embodiment of an element of enjoyment, and we know that there are two ways in which instances of string are:

String str = "Hello world"; String str2 = new string ("Hello World");

The way in which the former is literal is this: first look for "Hello World" in the string pool, if it does not exist, a "Hello World" is created in the string pool, and the reference to the object is returned to Str If it exists in the string pool, it is returned directly from the value in the string pool;

The concrete flow of the latter object is this: first look for "Hello World" in the string pool, if it does not exist, create one in the string pool, and then go to the heap to create a "Hello World" object, and return the reference in the heap to str2; If the object exists in the string pool, skip creating in the string pool and create a "Hello World" directly in the heap and return the reference.

Through the above you will understand:

Why "Hello world" = = "Hello World" is true; but new string ("Hello world") = = new String ("Hello Wrold") is false; "Hello Wo Rld "= = new String (" Hello World ") is also false;

In addition, the string Pool is a single-string-to-the-element factory, with the literal way of a single-element factory model. Everyone knows StringBuffer performance is better than string,stringbuilder performance is better than StringBuffer, but why still use string? The reason for this is that they say that the performance is when the string is modified, while the basic operation of the real character, and the string is obtained from the system, the string is the fastest and the least maintained object.

4. Pattern class Diagram

    1. Abstract enjoy meta (Flyweight) role: Enjoy the meta-interface, first declare the method of sharing the meta-object, and then it is important to declare the operation of the external state, such as the external state of the incoming method, setState, and so on, because the specific object is shared, but it is only part of the share, For external states, he is dynamically replaced by the client, thus achieving a state that is not shared.

    2. Specific concreteflyweight role: The specific object of the share, shared (the enjoy meta-factory class produces only one instance of this class, so similar to the singleton pattern, to the system to share an object), but also to encapsulate the internal state of the flyweight.

    3. Non-shared specific unsharedconcreteflyweight role: non-shared, specific, and not all flyweight objects need to be shared, and unshared objects are typically composite objects to a shared-object. The enjoy Meta factory class does not maintain instances of this class, and as long as the client needs instances of this class, the factory will be reborn from the new instance, so they are not sharing instances.

    4. Enjoy meta Factory (flyweightfactory) role: Enjoy meta factory, which is used primarily to create and manage shared-share objects, and to provide external access to shared-object-sharing interfaces.

    5. Client role: Enjoy meta client, the main task is to maintain a reference to the object, compute or store the external state of the object, or access shared and unshared objects.


5. Advantages of the Model

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

The enjoy meta-mode avoids the overhead of a large number of very similar instances, where it is sometimes necessary to generate a large number of fine-grained instances of the class to represent the data. If these instances can be implemented in addition to a few parameters are basically the same, and sometimes can be greatly reduced the number of classes that need to be instantiated. If you can move those parameters outside of the class instance and pass them in when the method is called, you can drastically reduce the number of individual instances by sharing.

The state that is required for the execution of the enjoy Meta mode is internal and possibly external, the internal state is stored in the Concreteflyweight object, and the external object should be considered to be stored or computed by the client object, which is passed to the flyweight object when the operation is invoked.

If a large number of objects are used in an application, and a large number of these objects cause the hated storage overhead, you should consider using the enjoy meta-mode, and the object's most states can be external states, if you delete the external state of the object, you can replace many groups of objects with relatively few shared objects, Consider using the enjoy meta mode at this point.


6. Comparison with similar models

Enjoy meta role is the smallest size of the shared object, in fact, personally think his problem is quite a lot of, such as multi-threaded control, shared nature is no problem, the number of customers do not share the possibility of confusion.

In addition, there are two non-shares in the enjoy meta-mode:

1) The non-shared specific privileges role, he is saying that the enjoy meta-factory does not need to maintain instances of this class, each time the client needs to be returned from a new instance. Well, it can be guessed that the implementation of the meta-factory is a map<string,flyweight> each time the client needs an instance, see if the key exists in the map, if it does not exist then a new return is stored in the map, If it is already present in the map, get the return directly from the map. Of course, it is also necessary to judge whether the obtained instance is unshared or shared, because unshared is the new one and does not require a map to be maintained.

2) external state, the so-called external state is for the sharing of specific roles, because it is an external state, so, is outside the control, each time the external need to change the state, so the specific Hengyuan role should provide external state modification methods.

Compared with other design patterns are sure to think of a singleton mode, the singleton mode is the whole system on an object, and this object has no external state, this is the biggest difference between the two, it can be said that the enjoyment of meta-role is more granular than a single case control of a class state, Singleton is only a special case of the enjoyment of the element.





The style of vegetable design--the mode of enjoying meta

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.