Java and mode: the metadata Mode

Source: Internet
Author: User
ArticleDirectory
    • Source code

Dr. Yan Hong's book "Java and patterns" describes the flyweight mode as follows:

In boxing, flyweight refers to the most lightweight model, that is, "Fly magnitude" or "rainfall level". Here we use the free translation of "Enjoy yuan mode", because it can better reflect the intention of the model. The metadata mode is the object structure mode. The metadata mode supports a large number of fine-grained objects efficiently in a shared manner.

String type in Java

In Java, the string type uses the metadata mode. The string object is of the final type and cannot be changed once it is created. In Java, string constants exist in the constant pool. Java ensures that a String constant has only one copy in the constant pool. String A = "ABC", where "ABC" is a String constant.

Public ClassTest {Public Static VoidMain (string [] ARGs) {string= "ABC"; String B= "ABC"; System. Out. println (=B );}}

The result in the above example is:True, Which indicatesBoth references a and B point to the same String constant "ABC" in the constant pool ". This design avoids unnecessary resource consumption when creating n multiple identical objects.

 

Structure of the metadata Mode

The metadata mode uses a shared object to avoid overhead of a large number of objects with the same content. The most common and intuitive overhead is the memory loss. The key to achieving shared object sharing is differentiation.Internal State)AndExternal 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 and cannot be shared as the environment changes. 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. They are independent of each other.

The metadata mode can be dividedSimple metadata ModeAndCompound metadata Mode.

Simple metadata Mode

In the simple metadata mode, all metadata objects can be shared.

The simple metadata mode involves the following roles:

●Abstract flyweight role:An abstract interface is provided to specify the methods to be implemented for all specific meta-roles.

●ConcreteFlyweight) Role:Implement the interface specified by the abstract metadata role. If there is an inner state, it must provide storage space for the inner state.

●Flyweightfactory role: This role is responsible for creating and managing the meta-role. This role must ensure that the object can be shared by the system. When a client object calls a metadata object, the metadata factory role checks whether a metadata object meets the requirements in the system. If you already have a metadata factory role, you should provide the existing metadata object. If the system does not have an appropriate metadata object, the metadata factory role should create a suitable metadata object.

Source code

Abstract meta-role class

Public InterfaceFlyweight {//A schematic method. The parameter state is an external state.Public VoidOperation (string State );}

In this example, a character-type intrinsicstate attribute represents the specific concreteflyweight class, and its value should be assigned when the object is created. After the object is created, all the internal statuses will not change.

If a metadata object has an external state, all external states must be stored on the client. When a metadata object is used, the client passes in the metadata object. There is only one external State. The parameter state of the operation () method is the external State passed in by the external.

 Public   Class Concreteflyweight Implements  Flyweight {  Private Character intrinsicstate = Null  ;  /**  * Constructor. The inner state is passed as a parameter *  @ Param  State  */      Public  Concreteflyweight (character state ){  This . Intrinsicstate = State ;}  /**  * The external State is used as a parameter to pass in the method and change the behavior of the Method * but does not change the inherent state of the object.  */ @ Override  Public   Void  Operation (string state ){  //  Todo auto-generated method stub System. Out. println ("Intrinsic State =" + This  . Intrinsicstate); system. Out. println ( "Extrinsic state =" + State );}} 

You must note that the client cannot instantiate a specific metadata class directly. Instead, you must use a factory object to obtain a metadata object using a factory () method. Generally, the metadata factory has only one object in the system. Therefore, you can use the singleton mode.

When the client needs to simply enjoy the metadata object, it needs to call the factory () method of the metadata factory, and pass in the inherent state of the simple metadata object, which is generated by the factory method.

 Public  Class  Flyweightfactory {  Private Map <character, flyweight> files = New Hashmap <character, flyweight> ();  Public  Flyweight Factory (character state ){  //  Search for objects from the cache first Flyweight fly = Files. Get (State );  If (Fly = Null  ){ //  If the object does not exist, create a new flyweight object. Fly = New  Concreteflyweight (State );  //  Add the new flyweight object to the cache.  Files. Put (State, fly );}  Return  Fly ;}} 

Client type

 Public   Class  Client {  Public   Static  Void  Main (string [] ARGs ){  //  Todo auto-generated method stub Flyweightfactory factory = New  Flyweightfactory (); flyweight fly = Factory. Factory ( New Character ('A' ); Fly. Operation ( "First call" ); Fly = Factory. Factory ( New Character ('B' ); Fly. Operation ( "Second call"); Fly = Factory. Factory ( New Character ('A' ); Fly. Operation ( "Third call" );}} 

Although the client has applied for three metadata objects, there are only two actually created metadata objects. This is the meaning of sharing. The running result is as follows:

 

Compound metadata Mode

In the simple meta mode, all the meta objects are just meta objects, that is, they can be directly shared. There is also a complicated situation where some simple elements are combined using the synthesis mode to form a composite object. Such compound object objects cannot be shared, but they can be divided into simple object objects, while the latter can be shared.

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.