23 Design Modes (21)-Enjoy meta mode

Source: Internet
Author: User
Tags abstract constant stub
23 Design modes (21)-Enjoy meta mode

In the book "Java and Patterns" of Dr. Shanhong, this describes the Flyweight pattern: ❈

Flyweight in boxing is the most lightweight, that is, "The fly level" or "rainfall level", where the choice to use the "enjoy meta-mode" of the free translation, because it is more reflective of the intention of the model. The enjoy meta mode is the structure mode of the object. The enjoy meta mode efficiently supports a large number of fine-grained objects in a shared manner. ❈

String types in Java

In the Java language, the string type is the use of the enjoy meta pattern. The string object is the final type, and the object cannot be changed once it is created. In Java, where string constants are present in a 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 class Test {
public static void Main (string[] args) {
String a = "abc";
String B = "abc";
System.out.println (A==B);
}
}


The result in the above example is: true, which means that both A and B two references point to the same string constant "ABC" in the Constant pool. Such a design avoids the unnecessary amount of resource consumption generated when creating n many identical objects.


Structure of the enjoy meta-mode

The enjoy meta mode takes a share to avoid the overhead of having the same content object in large numbers. The most common and intuitive kind of overhead is the loss of memory. The key to sharing the shared meta object is to differentiate between the intrinsic state (the Internal state) and the outer States (External).

An intrinsic state is stored inside the element of the object and does not vary depending on the environment. Therefore, a share can have an intrinsic state and can be shared.

A foreign State is changed with the change of the environment and cannot be shared. The outer state of the object must be saved by the client, and then passed in to the inside of the object when it is needed after the object is created. The outer state can not affect the intrinsic state of the objects, they are independent of each other.

The enjoy meta-mode can be divided into two forms: the simple-enjoy meta-mode and the compound-sharing model.


Simply enjoy meta mode

In the simple-to-enjoy mode, all the objects of the object can be shared.

The roles involved in the simple-to-enjoy meta-mode are as follows:

Abstract enjoy meta (Flyweight) role: An abstract interface is given to specify the methods that are required to be implemented for all of the specific enjoyment meta-roles.

Specific Concreteflyweight role: implements the interface defined by the abstract privileges meta role. If there is an intrinsic state, you must be responsible for providing storage space for the intrinsic state.

Enjoy meta-factory (flyweightfactory) Role: This role is responsible for creating and managing the rewards role. This role must ensure that the sharing meta-object can be properly shared by the system. When a client object invokes an object, the enjoy Meta factory role checks to see if there is already an eligible object in the system. If it is already available, the meta-factory role should provide the existing one, and if the system does not have an appropriate element of the object, the enjoy meta-factory role should create a suitable object for the privilege.


Source

Abstract enjoy Meta role class

Public interface Flyweight {
A schematic method in which the state of the parameter is external
public void operation (String state);
}


The concreteflyweight has an intrinsic state, in this case the Intrinsicstate attribute of a character type is represented, and its value should be given when the element object is created. All the intrinsic states are not changed after the object is created.


If an object has an external state, all external states must be stored on the client, and the client is passed in with the object of the privilege when the element is used. There is only one external state, and the operation () method's parameter state is an external incoming foreign.

public class Concreteflyweight implements Flyweight {
Private Character intrinsicstate = null;
/**
* constructor, intrinsic State as parameter passed in
* @param state
*/
Public Concreteflyweight (Character state) {
This.intrinsicstate = State;
}

/**
* Outside the state as a parameter in the method, change the behavior of the method,
* But does not change the intrinsic 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);
}
}


Enjoy the meta-factory role class, it must be noted that the client cannot instantiate a specific class of classes directly, but must pass a factory object, using a factory () method to get the object of the privilege. In general, there is only one in the entire system for the enjoy Meta factory object, so you can also use singleton mode.


When the client needs to simply enjoy the meta-object, it is necessary to call the factory () method of the Meta factory and pass in the intrinsic state of the desired simple-to-enjoy meta-object, and the factory method produces the desired object.

public class Flyweightfactory {
Private map<character,flyweight> files = new hashmap<character,flyweight> ();

Public Flyweight Factory (Character state) {
Find an object from the cache first
Flyweight fly = Files.get (state);
if (fly = = null) {
Creates a new flyweight object if the object does not exist
Fly = new Concreteflyweight (state);
Add this new flyweight object to the cache
Files.put (state, fly);
}
return fly;
}
}


Client class

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 requested three of the objects, only two are actually created, which is the meaning of sharing. The results of the operation are as follows:


Compound enjoy meta mode

In the simple-to-enjoy meta-mode, all the objects of the object are simply to enjoy the meta-object, which means they can be shared directly. There is also a more complex situation, the use of a few simple-to-enjoy composite model to compound, to form a compound to enjoy meta-objects. Such compound-sharing objects cannot be shared by themselves, but they can be decomposed into simple-to-enjoy meta-objects, while the latter can be shared.


The roles involved in the compound-sharing role are as follows:

Abstract enjoy meta (Flyweight) role: An abstract interface is given to specify the methods that are required to be implemented for all of the specific enjoyment meta-roles.

Specific Concreteflyweight role: implements the interface defined by the abstract privileges meta role. If there is an intrinsic state, you must be responsible for providing storage space for the intrinsic state.

Compound Concretecompositeflyweight Role: the objects represented by the compound-enjoy meta-role are not shareable, but a composite-object can be decomposed into a combination of multiple, simple-to-enjoy meta-objects. The compound privilege role is also known as a non-shareable, shared meta object.

Enjoy meta-factory (flyweightfactory) Role: This role is responsible for creating and managing the rewards role. This role must ensure that the sharing meta-object can be properly shared by the system. When a client object invokes an object, the enjoy Meta factory role checks to see if there is already an eligible object in the system. If it is already available, the meta-factory role should provide the existing one, and if the system does not have an appropriate element of the object, the enjoy meta-factory role should create a suitable object for the privilege.


Source

Abstract enjoy Meta role class

Public interface Flyweight {
A schematic method in which the state of the parameter is external
public void operation (String state);
}


Specific enjoy meta role classes

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.