Reuse objects-the metadata mode (2)

Source: Internet
Author: User

The structure of the share mode is complex. It is generally used in combination with the factory mode. Its structure diagram contains a share factory class. Its structure is 14-3:

 

Figure 14-3 structure of the metadata Mode

The structure chart contains the following roles:

● Flyweight (Abstract metadata class): it is usually an interface or abstract class. In the abstract metadata class, the public methods of the specific metadata class are defined, these methods can provide external users with internal data (internal status) of the object, and can also be used to set external data (external status ).

● Concreteflyweight (a specific metadata class): it implements an abstract metadata class, and its instance is called a metadata object. In a specific metadata class, it provides a storage space for the internal status. Generally, we can design a specific metadata Class Based on the singleton mode to provide a unique metadata object for each specific metadata class.

● Unsharedconcreteflyweight (non-shared object meta class): Not all abstract object meta classes must be shared. Non-shared object meta classes cannot be shared; an object that does not share a specific metadata class can be created directly through instantiation.

● Flyweightfactory: A metadata factory class is used to create and manage metadata objects. It is designed for abstract Metadata classes and stores various types of metadata objects in a metadata pool, the metadata pool is generally designed as a set that stores "key-value pairs" (or other types of sets) and can be designed in combination with the factory mode. When a user requests a specific metadata object, the metadata factory provides an instance that has been created in the metadata pool or creates a new instance (if it does not exist), returns the newly created instance and stores it in the metadata pool.

The meta-factory class is introduced in the meta-mode. The meta-factory class provides a meta-pool for storing the meta-objects. When users need objects, they first obtain them from the meta-pool, if the metadata pool does not exist, a new metadata object is created and returned to the user. The new object is saved in the metadata pool. The typical metadata Factory Code is as follows:

Class flyweightfactory {

// Define a hashmap to store the object to be shared.

Private hashmap flyweights = newhashmap ();

Public flyweight getflyweight (string key ){

// If the object exists, it is directly obtained from the metadata pool

If (flyweights. containskey (key )){

Return (flyweight) flyweights. Get (key );

}

// If the object does not exist, create a new object and add it to the shared object pool. Then, return

Else {

Flyweight fw = newconcreteflyweight ();

Flyweights. Put (Key, Fw );

Return FW;

}

}

}

The design of the metadata class is one of the key points of the metadata mode. In the metadata class, internal states and external states must be processed separately. Generally, internal states are used as member variables of the metadata class, the external status is added to the metadata class by injection. The typical metadata class code is as follows:

Class flyweight {


//
The internal status intrinsicstate is used as the member variable, and the internal status of the same object is consistent.

Private string intrinsicstate;

Public flyweight (string intrinsicstate ){

This. intrinsicstate = intrinsicstate;

}

// The external State extrinsicstate is set by the outside when used, and is not saved in the meta object. Even for the same object, different external states can be passed in for each call.

Public void operation (string extrinsicstate ){

......

}

}

[Author: Liu Wei http://blog.csdn.net/lovelion]

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.