Structural patterns of design patterns

Source: Internet
Author: User

design patterns are divided into three main types: creation, structure and behavior. This article describes the structural pattern.

The structure mode includes adapter mode, bridge mode, combination mode, decoration mode, appearance mode, enjoy meta-mode and proxy mode.

Adapter Mode: transforms the interface of a class into another interface that the customer wants. The adapter mode makes it possible for those classes that would otherwise not work together because of incompatible interfaces to work together.

Application: The system data and behavior are correct, but the interface does not match, we should consider the adapter, the purpose is to make an existing object outside the control range to match an interface. The adapter pattern is primarily used in situations where you want to reuse some existing classes, but the interfaces are inconsistent with the reuse environment requirements.

Bridging mode: separates the abstract part from its implementation, so that they can all change independently.

Features: The core intent of bridging mode is to separate these implementations from each other and allow them to vary. This makes each implementation change without affecting other implementations, thus achieving the goal of responding to change.

composition mode: combines objects into a tree structure to represent a "partial-whole" hierarchy, combining patterns that allow users to be consistent with the use of individual objects and composite objects.

Application: When the requirements reflect the structure of a part and a whole hierarchy, and if you want users to be able to ignore the difference between a combined object and a single object, you should consider combining patterns when using all the objects in a composite structure uniformly.

decorating mode: dynamically adds some additional responsibilities to an object. For added functionality, the adornment mode is more flexible than generating subclasses.

Application: When the system needs new functionality, it is adding new code to the old class. These new codes usually adorn the core duties or main behaviors of the original class, and the things that are added to the main class are simply to satisfy the needs of some special behavior that will only be performed under certain circumstances. Decoration mode provides a great solution, which puts each function to be decorated in a separate class, and lets the class wrap the object it is decorating, so that when special behavior is required, the customer code can selectively and sequentially use the adornment function to wrap the object at run time.

Benefits: (1) Remove the decoration function from the class, so that the original class can be simplified. (2) effectively separates the core duties of the class from the decorative function areas. It can also remove repetitive decorative logic from related classes.

appearance mode: provides a consistent interface for a set of interfaces in a subsystem, and the appearance pattern defines a high-level interface that makes this subsystem easier to use.

Application: (1) in the initial stage of design, we should be aware of the different two levels of separation, layer and layer to establish the appearance of facade. (2) In the development stage, subsystems tend to become more and more complex because of the evolution of continuous reconstruction, and the increase of appearance facade can provide a simple interface and reduce the dependence between them. (3) When maintaining a large legacy system, it is possible that the system is very difficult to maintain and expand. Develop an appearance facade class for the new system to provide a clearer and simpler interface for designing rough or highly complex legacy code, allowing the new system to interact with facade objects, facade all complex work with legacy code.

enjoy meta-mode : effectively supports a large number of fine-grained objects for use in shared technology.

Application: If an application uses a large number of objects, and a large number of these objects cause a lot of storage overhead should be considered to use, also is that most of the state of the object can be external state, if you delete the external state of the object, you can use a relatively small number of shared objects to replace many groups of objects, Consider using the enjoy meta mode at this point.

proxy mode : provides a proxy for other objects to control access to this object.

Application: (1) remote agent, that is, for an object in a different address space to provide local representation. This can hide the fact that an object exists in a different address space. (2) virtual agent, is to create expensive objects as needed. It is the real object that takes a long time to store the instantiation. (3) A security agent that controls the permissions of real objects when they are accessed. (4) can only guide, refers to when the real object is called, the agent handles other things.

Structural patterns of design patterns

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.