Sharing Mode for normal girls

Source: Internet
Author: User

Class Relationship Diagram

Class role description
Abstract flyweight 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.

Concreteflyweight role: implements the interface defined 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 within the system.

Flyweightfactory role: this role is responsible for creating and managing the metadata. 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 there is a composite requirement for the metadata object 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.

Client role: This role must maintain a reference to all metadata objects. This role needs to store the external status of all the meta objects on its own.

Intention
Flyweight is the most lightweight in boxing, that is, "Fly magnitude", which is translated by some authors as "Yu magnitude ". Here, the "share mode" is used to better reflect the intention of the 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 ). The internal state is stored inside the object and does not change with the environment. Therefore, the status can be shared. The 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 status and internal status are independent of each other.

Applicability
When all of the following conditions are met, you can consider using the enjoy mode:
· A system has a large number of objects.
· These objects consume a large amount of memory.
· Most of the statuses of these objects can be externalized.
· 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.
· The software system does not rely on the identities of these objects. In other words, these objects can be undistinguished.
The system that meets the preceding conditions can use a shared object.
Finally, you need to maintain a table that records all the existing metadata in the system in the metadata-sharing mode, which consumes resources. Therefore, the metadata mode should be used only when there are enough metadata instances available for sharing.

Niu's example
Flyweight-I send text messages to mm every day, and my fingers are exhausted. I recently bought a new mobile phone. I can put some frequently-used sentences in my mobile phone. When I use them, I can take them out directly, add the MM name to the front and you will be able to send the message without a single word. The shared sentence is flyweight, And the MM name is the extracted external feature, which is used according to the context.

Niu's code

Using system;
Using system. collections;
// Abstract the flyweight role
Public abstract class abstract text message
{
Public abstract void sending ();
}
// If the actual meta-role has an internal state, it must provide storage space for the internal state. The inherent state of the object must be unrelated to the environment of the object, so that the object can be shared within the system.
Public class SMS: Abstract SMS
{
String MSG;
Public SMS (string strmsg)
{
MSG = strmsg;
}
Public override void ()
{
Console. writeline (MSG );
}
}
// The metadata factory class when a client object calls a metadata object, the metadata factory role checks whether there is a composite metadata object 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.
Public class mobile phone
{
Private hashtable SMS template = new hashtable ();
// If the inner state is a parameter, check whether the factory list contains objects in the same inner state. If yes, return this object; otherwise, create a new object.
Public abstract SMS getmsg (string MSG)
{
If (! SMS template. containskey (MSG ))
SMS template. Add (MSG, new SMS (MSG ));
 
Return (Abstract SMS) text message template [MSG]);
}
Public int template capacity ()
{
Return SMS template. count;
}
}
// This role must maintain a reference to all the metadata objects. This role needs to store the external statuses of all the object objects
Public class client
{
New private static mobile phone;
Private Static int msgcount = 0; // external store status
Public static void main ()
{
New Mobile Phone = new mobile phone ();
Text message ("Lily", "I love you! ");
Text message ("Mary", "let's be friends! ");
Text message ("Lisa", "are you free tonight? ");
Text message ("Mary", "I love you! ");

Console. writeline ("sent" + msgcount. tostring () + "short message! ");
Console. writeline ("Total in mobile phones" + convert. tostring (new mobile phone. template capacity () + "template information! ");
}

Private Static void text message (string mm, string MSG)
{
Abstract SMS Short Message = new mobile phone. getmsg (MSG );
Console. Write (mm );
Short Message. Send ();
 
Msgcount ++;
}
}

Meta Mode
Enjoy mode: flyweight is the most lightweight in boxing. The metadata mode efficiently supports a large number of fine-grained objects in a shared manner. The key to the sharing mode is to distinguish between the internal and external states. The internal status is stored in the shared element and will not change with the environment. The external state changes with the change of the environment. The external status cannot affect the internal status. They are independent of each other. The States that can be shared and those that cannot be shared are separated from the conventional classes to remove the States that cannot be shared from the classes. The client cannot directly create shared objects, but should use a factory object to create shared objects. The metadata mode greatly reduces the number of objects in the memory.

Advantages and disadvantages
The advantage of the Meta mode is that it greatly reduces the number of objects in the memory. However, it also pays a very high price to achieve this:
· The metadata-sharing mode 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.
· The metadata mode externalizes the state of the object, and reads the external State to slightly extend the running time.

Description
Transactions can be divided into internal and external states. The internal State identifies the internal characteristics of an object, which remains relatively stable. The external State implements the external function of the object, which changes with the changes of the external environment. The internal and external states of the object are relatively independent and will not affect each other.
Here are some examples of the application of the metadata-sharing model, which are very popular on the Internet.
Example of coffee stalls
Coffee stall is used in a series of coffee "Flavors )". The guests purchase coffee at the booth. All coffee is put on the desk, and the guests leave the booth after they get the coffee themselves. Coffee has an inherent state, that is, the flavor of coffee. Coffee has no environmental factors, that is, there is no external presence. If the system creates an independent object for each cup of coffee, many small objects need to be created. In this way, it is better to divide the coffee by type (I .e. "flavor"), and create only one object for each flavor of coffee and share it.
In terms of coffee vendors, all coffee can be classified into capucino and espresso by "flavor". No matter how many coffee cups are sold, they are all the same and cannot be distinguished. Sharing means sharing of coffee flavor and sharing of manufacturing methods. Therefore, for coffee stalls, The enjoy mode does not need to be separately modulated for each copy. The stall owner can
When needed, one-time coffee is made to produce a certain flavor of coffee that is enough for sale each day.

Editor example
It is widely used in the editor system. 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 in the text, although these letters
The location is different from the font style, but all these places use the same letter object. In this way, letter objects can be shared throughout the system.

Example of employee objects
An employee's name, gender, and other personal conditions are relatively stable and belong to the internal status of the employee. The employee's role, position, and salary change along with the environment and belong to the external status.
General explanation:
Abstract flyweight role: defines basic behaviors of employee objects
Actual meta-Role: has an internal status attribute, which is assigned a value during initialization.
Share factory role: manages the actual share role. If the client needs to create a new share role, first check whether the actual share role with the same internal attributes already exists, if yes, an existing object is returned. Otherwise, a new object is created.
 

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.