Design Pattern: flyweight Mode

Source: Internet
Author: User

Http://www.35java.com/zhibo/forum.php? MoD = viewthread & tid = 276 & extra = Page % 3d2

 

InGof booksThe purpose of Flyweight is to use the sharing technology so that some fine-grained objects can be shared.

The description of Flyweight in the Oxford dictionary is "boxer of the lightest class ". Is it a lightweight boxer? In fact, this part should be interpreted as "thelightestclass". In a lightweight category, the objects produced by this category can be shared in each context and display the appearance of the objects according to the information on the occasion.

The example in the book is the character object in the document editor. If each character object contains different information, such as characters, sizes, and fonts, think about it.ArticleHow many characters may appear in a text. If we use an object for every element to fully describe the information about it, how much memory will a text consume ?! Character itself should be able to share, and different information such as size and font should be set separately.

When considering a large number of objects with similar properties, the information about the object is divided into two parts: the internal state (intrinsic) and the external State (extrinsic ). In the preceding example, a character is in an internal state, while different information such as size and font is in an external state.

For more details, the internal state is the part of the message that the object can share. For example, when an English string is drawn, the re-overwriting character part is the internal state, such as "ABC isbac ", the character information section of A, B, and C does not need to be directly stored in character objects. It is a shared part. You can store these reusable characters in the flyweightpool.

The external State is the context of an object, for example, the font information and location information when the element is drawn. When the element is drawn, the shared flyweight is first found in the flyweight pool, then, find the corresponding information (such as the font, size, and position) in the scenario ).

In fact, anyone who has learned Java must have used the advantages of using the flyweight mode in Java. You must know that if you use the following methods in your program to declare it, it actually points to the same string object:
String str1 = "flyweight ";
String str2 = "flyweight ";
System. Out. println (str1 = str2 );

The program execution result is displayed as true. in Java, a string pool is maintained. For some string objects that can be shared, in the stringpool, the system first checks whether the same string content (with the same character) exists. If yes, the system returns the data directly instead of creating a new String object to reduce memory consumption.

Let's take a look at the example of the string intern () method. Let's take a look at its API description section:
Returns a canonical representation for the string object.

A pool of strings, initially empty, is maintained privately by the class string.

Whenthe intern method is invoked, if the pool already contains a stringequal to this string object as determined by the equals (object) method, then the string from the pool is returned. otherwise, this stringobject is added to the pool and a reference to this string object isreturned.

In fact, this section describes how the flyweight mode works. It is clearer to use an instance:

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.