Java and mode Learning Series-Metadata Mode

Source: Internet
Author: User

 

I, Structure of the flyweight Mode

The object metadata mode is the structure mode of objects. The metadata mode efficiently supports a large number of fine-grained objects in a shared manner.

The key to enabling shared object sharing is to differentiate the internal state and external State ).

An internal state is stored inside the object, and does not change with the environment. Therefore, a shared object can have an internal state and can be shared.

An external state changes with the environment and cannot be shared. The external status of the object must be saved by the client. After the object is created, the object must be uploaded to the object.

The external State does not affect the inherent state of the object. In other words, they are independent of each other.

The metadata sharing mode can be divided into the simple metadata sharing mode and the compound metadata sharing mode.

As shown in the simple metadata Mode:

Structure of the compound metadata mode:

II, Implementation of the metadata Mode

Simple metadata Mode

Public abstract class flyweight {

Abstract Public void
Operation (string State );

}

/**

* A shared object has an internal state, which is represented by a character-type intrinsicstate attribute.

* Its value should be assigned when the object is created. After the object is created, all the internal statuses will not change.

* If the object has an external State, all the external states must be stored on the client. When using the object,

* Then, the client passes in the metadata object.

*/

Public class concreteflyweight extends flyweight {

 

Private Character
Intrinsicstate = NULL;

Public
Concreteflyweight (character state ){

This. intrinsicstate
= State;

}

@ Override

// The external state changes the behavior of the method as a parameter passed in method, but does not change the inherent state of the object.

Public void
Operation (string state ){

System. Out. println ("/nintrinsic
State ="

+ Intrinsicstate + ", extrinsic
State "+ State );

}

}

/**

* The client cannot directly instantiate a specific metadata class, but must use a factory object,

* Use a factory () method to obtain the metadata object. Generally, there is only one object in the whole system.

* Therefore, you can use the singleton mode.

*/

Public class flyweightfactory {

 

Private hashmap flies
= New hashmap ();

Private flyweight
Lnkflyweight;

Public
Flyweightfactory (){}

Public flyweight
Factory (character state ){

If (flies. containskey (State ))

Return
(Flyweight) flies. Get (State );

Else {

Flyweight
Fly = new concreteflyweight (State );

Flies. Put (state,
Fly );

Return
Fly;

}

}

}

Implementation of the composite metadata Mode

Only the concretecompositeflyweight code is listed here:

/**

* 1. A compound object is composed of a simple object that is shared by a compound object. Therefore, it provides an aggregation management method such as add.

* Because a composite object has different clustering elements, these elements are added after the composite object is created,

* This means that the state of the compound object is changed, so the compound object cannot be shared.

* 2. The composite metadata role implements the interface specified by the abstract metadata role, that is, the operation method. This method has a parameter

* Represents the external state of the composite object, and the external State of all elements of a composite object.
Both are composite

* The inherent status of a simple object that is contained in the object is generally not equal, otherwise there will be no use value.

*/

Public class concretecompositeflyweight extends flyweight {

 

Private hashmap flies
= New hashmap (10 );

Private flyweight
Flyweight;

Public
Concretecompositeflyweight (){}

Public void
Add (character key, flyweight fly ){

Flies. Put (key,
Fly );

}

Public void
Operation (string extrinsicstate ){

Flyweight
Fly = NULL;

For (iterator
It = flies. entryset (). iterator (); it. hasnext ();){

Map. Entry
E = (Map. Entry) it. Next ();

Fly
= (Flyweight) E. getvalue ();

Fly. Operation (extrinsicstate );

}

}

}

III, Usage

1. A system has a large number of objects.

2. These objects consume a large amount of memory.

3. Most of the statuses of these objects can be externalized.

4. These objects can be divided into many groups according to the inherent state. When the external object is removed from the object, each group can be replaced with only one object.

5. The software system does not depend on the identities of these objects. In other words, these objects can be undistinguished.

The advantage of the meta-mode is that it greatly reduces the number of objects in it. However, it pays a high price to achieve this: first, the metadata-sharing model makes the system more complex. In order for objects to be shared, some States need to be externalized, which complicate the logic of the program. Second, the metadata mode externalizes the state of the object, and reads the external State to slightly extend the running time.

Iv. Example of the metadata sharing mode

A text editor often provides many types of fonts, and the common practice is to make every letter a metadata object. The internal state of the object is this letter, while other information such as the position and style of letters in the text is the external state. For example, Letter a may appear in many places of the text. Although the location of these letters A is different from the model style, all these places use the same letter object. In this way, letter objects can be shared throughout the system.

 

 

 

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.