Design Mode Reading Notes (7)

Source: Internet
Author: User

2004-12-1

Flyweight Mode

Name: Metadata mode, lightweight mode, and feather Mode

Problem:

It is good to use objects to express the application. However, if a document editor processes text, all processed letters can be viewed as objects (Object-Oriented). However, in this way, a huge amount of objects will be generated and system resources will be greatly consumed. How can we share these objects with the same features?

 

Solution:

Defines an object called flyweight, which is a shared object. It is used in different contexts and can be seen as opposite objects. However, flyweight cannot make any assumptions about its context. The internal status exists in flyweight and contains the opposite context. The information can be shared. The external status depends on the context and cannot be shared, the internal object is responsible for passing the external status to flyweight when necessary. In this way, there is a state that all objects can share and an external State that is different from other objects. For example, Java's String object is an application of the metadata-sharing mode.

 

Effect:

This greatly reduces the number of objects and saves space. However, management is complicated. You must be extremely careful when distinguishing between external and internal states.

 

Figure:


 

 

 

 

 

Proxy Mode

Name: proxy mode, proxy, surrogate

Problem:

Considering embedding images and other large elements in an editing system, creating these elements consumes a lot of resources, but we know that it takes a lot of time to open a document, we should avoid creating all images when opening them, and may not see them in the display part. This means that we can create an object with high overhead as needed. But how can we express this object in the document?

 

Solution:

We designed a proxy object to represent large elements such as the graphic type. Instead of creating and displaying the elements when they are actually needed. The proxy only represents the size of the image storage and other information when the document is continuous.

 

Effect:

Coordinates callers and callers, reducing coupling.

 

Figure:

 

 

Structural Patterns are very similar, but the main difference is that they are different in solving different problems and have different purposes.

The adapter mode mainly solves the mismatch problem between two existing interfaces.

In the bridge mode, the abstract interface and its multiple parts are bridging, allowing modifications to implement this mode class.

Similar to decorator, composite generates new objects based on Recursive combinations. However, the decorator mode does not require subclass generation to add responsibilities to objects to avoid subclass addition. composite is designed to construct classes, so that multiple related formations can be processed in a unified manner.

 

Related Article

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.