Design pattern of C # design pattern (Flyweight)

Source: Internet
Author: User
Tags abstract

One, the element (Flyweight) mode

Flyweight in boxing competition, the most lightweight, that is, "fly level", some authors translated as "feather grade." This use of the "element mode" is more reflective of the intent of the pattern.

The privilege mode supports a large number of fine-grained objects efficiently and in a shared way. The key to sharing a shared object is to distinguish between the intrinsic state (Internal state) and the Outer Yun status (External states). The intrinsic state is stored inside the object and does not change as the environment changes. So the intrinsic state can be shared.

The state of the outer Yun is a state that is changed and cannot be shared with the environment. The external state of the privilege object must be saved by the client and then passed into the inside of the object when it is needed, after the object is created. The external state and the intrinsic state are independent of each other.

The application of the pattern of privilege

The element mode is used extensively in the editor system. A text editor often provides a wide variety of fonts, and the usual practice is to make each letter a single object. The intrinsic state of the object is the letter, and other information, such as the position of the letter in the text and the style of the font, is the outer state. For example, the letter a may appear in many parts of the text, although the position of the letters a differs from the font style, but all of these places use the same alphabetic object. As a result, alphabetic objects can be shared throughout the system.

Second, the structure of the simplex mode

In the exclusive element mode, all of the privilege objects can be shared. The following roles are involved in the simplex mode:

Abstract (Flyweight) Role: This role is a superclass of all the specific classes of the class, specifying the public interfaces that need to be implemented for these classes. Operations that require the presence of the External state can be passed in as arguments by invoking a business method.

The specific (concreteflyweight) role: Implements the interface specified by the abstract privilege role. If you have an intrinsic state, you must be responsible for providing storage space for the intrinsic state. The intrinsic state of the privilege object must be independent of the environment around which the object is in, so that the object of the privilege can be shared within the system.

The flyweightfactory role: This role is responsible for creating and managing the privilege role. This role must ensure that the privilege object can be properly shared by the system. When a client object invokes a privilege object, the Meta factory role checks to see if there is already a compound requirement in the system. If it is already available, the Henry Factory role should provide this existing object, and if the system does not have an appropriate object to enjoy, the Meta factory role should create a suitable object for the privilege.

Client role: This role needs to maintain a reference to all the object of the privilege. This role needs to store the outer state of all the objects of the privilege itself.

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.