Java Design Pattern 10: flyweight

Source: Internet
Author: User

The metadata mode is the object structure mode.

The metadata mode supports a large number of fine-grained objects efficiently 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 can change as the Environment Changes 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.

Simple metadata Mode

Compound metadata Mode

 

From the preceding class diagram, we can see that in the composite meta mode, the meta object constitutes the merging mode. Therefore, the compound metadata mode is actually a combination of the simple metadata mode and the merging mode.

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

1. Abstract meta-role:This role is a superclass of all the specific Metadata classes, and defines the public interfaces to be implemented for these classes. Operations that require external State can be passed in as parameters by calling commercial methods.
2. Specific metadata roles:Implement the interface specified by the abstract metadata role. If there is an inner state, it must provide storage space for the inner state. The inherent state of the object must be unrelated to the environment of the object, so that the object can be shared in the system.
3. Enjoy the role of the Yuan Factory: 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 the system has a composite metadata object. 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.
4. Client role:This role must maintain a reference to all the metadata objects. This role needs to store the external status of all the meta objects on its own.

Sample Code:

The internal state is saved by the intrinsicstate. After an object is created, all the internal states cannot be changed.

The external status is set through operation, which is called and changed by the client as needed.

In the following code, three object categories are applied, but only two object categories are created. This is the meaning of shared objects.

Package com. javapatterns. flyweight. Simple;

Public class client
{
Private Static flyweightfactory factory;

Static public void main (string [] ARGs)
{
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 ");

Factory. checkflyweight ();
}
}

 

 

Package com. javapatterns. flyweight. Simple;

Import java. util. Map;
Import java. util. hashmap;
Import java. util. iterator;

Public class flyweightfactory
{
Private hashmap flies = new hashmap ();

Private flyweight lnkflyweight;

Public flyweightfactory (){}

Public synchronized 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;
}
}

Public void checkflyweight ()
{
Flyweight fly;
Int I = 0;

System. Out. println ("\ n =========== checkflyweight () =============== ");
For (iterator it = flies. entryset (). iterator (); it. hasnext ();)
{
Map. Entry E = (Map. Entry) it. Next ();
System. Out. println ("item" + (++ I) + ":" + E. getkey ());
}
System. Out. println ("=========== checkflyweight () ================= ");
}

}

 

 

 

Package com. javapatterns. flyweight. Simple;

Public class concreteflyweight extends flyweight
{

Private Character intrinsicstate = NULL;

Public concreteflyweight (character state)
{
This. intrinsicstate = State;
}

Public void operation (string state)
{
System. Out. Print ("\ nintrinsic state =" + intrinsicstate +
", Extrinsic state =" + State );
}
}

 

 

Package com. javapatterns. flyweight. Simple;

Abstract Public class flyweight
{
Abstract Public void operation (string State );
}

 

 

 

Compound metadata mode:

It is easy to understand from the graph, but the code in the book is really dizzy, just skip it first.
 

 

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.