. NET design Pattern instances (Flyweight pattern)

Source: Internet
Author: User
Tags abstract

A brief introduction to the mode of Brief (Introduction)

(Flyweight pattern), the use of shared technology to effectively support a large number of fine-grained objects.

Use sharing to support large numbers of fine-grained objects.

The privilege pattern avoids the overhead of a large number of very similar classes. In programming, it is sometimes necessary to generate a large number of fine-grained class instances to represent data. If these instances are found to be identical except for a few of the parameters, the number of classes that need to be instantiated can sometimes be reduced by a large amount. If you can move these parameters outside the class instance and pass them in when the method is called, you can drastically reduce the number of individual instances by sharing.

The internal state and external state of the Privilege object:

An internal state, a shared part that is within the object of the element and that does not change with the environment.

An external state that changes with the environment and cannot be shared.

Ii. issues addressed (What to Solve)

If an application uses a large number of objects, and a large number of these objects cause a large storage overhead, consider using the pattern of privileges.

When most of the state of an object is an external state, if you delete the external state of an object, you can replace many groups of objects with a relatively small share object, and you can also consider using the pattern of privileges.

Analysis of the mode of enjoying meta-element

1. Pattern structure of the element

Flyweightfactory class: The Henry Factory, which is used to create and manage flyweight objects. If the requested flyweight object exists, how to return an object that already exists. Otherwise, a new object is created to return.

Flyweight class: The abstract class, through which flyweight can accept and interact with the external state.

Unsharedconcreteflyweight class: Flyweight subclasses that do not need to be shared. The flyweight interface does not force sharing.

Concreteflyweight class: Implement the Meta abstract class to add storage space for the internal state.

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.