Design Patterns (5) Structural Patterns Overview

Source: Internet
Author: User
1. Bridge Mode

Core Ideas:Abstract The interface (interface class) and isolate the client (Operation class) through inheritance to achieve decoupling.

Application scenarios:Both a sir and bsir are responsible for two modules of a system. A needs to call the implementation of B. later tests show that the module efficiency of B sir is too low, therefore, B needs to optimize and rewrite its own code. If a and B do not have an interface method defined in advance, it is very likely that B needs to modify the code after optimization. In this case, the Bridge Mode comes in handy. A and B need to first define the call relationship between each other and implement the call using the interface class. In this way, we only need to ensure that the interface remains unchanged. To modify B, we only need to add a subclass or modify the subclass method. In this case, a does not need to make any changes.

2 adapter Mode

Core Ideas:The adapter mode provides the ability to convert interfaces of known classes (third-party libraries) into the desired interfaces, so that classes that cannot work together due to interface incompatibility can work together.

Application scenarios:My brother department needs us to provide an interface class to create a file. Now we have a class to implement this function, but the interface is not the same as they are, so we can implement this class through the adapter method.

3. decorator Mode

Core Ideas:Adds an Interface Class (decoration) to a subclass that may have many different actions as its parent class.

Application scenarios:In fact, it is to separate these many irrelevant sub-classes from component, and at the same time there is a unified parent class decorator as the Management Interface (Bridge Mode? ).

 

4. Composite Mode

Core Ideas:Combine objects into a tree structure (parent and child have the same attribute) to represent the "Parent-Child" hierarchy, and parent and child have the same interface information.

Application scenarios:When processing data with a tree structure, the combination mode provides a better solution. For example, for data in XML format, we can regard it as a tree. A node (tree branch) has the following characteristics: it is a child node (the child node of the tree trunk) it may also be the parent node (the parent node of the leaf) (equivalent to the role of the trunk ). These nodes have the same attributes as the parent node, and are designed in combination mode.

5 Share (flyweight) Mode

Core Ideas:The sharing mode is a resource pool, which is used to reduce overhead that requires repeated creation with high density.

 

Application scenarios:This mode can be used if a resource pool is required.

 

6. Facade Mode

 

Core Ideas:Provides a consistent interface for a group of interfaces in the subsystem, which makes the subsystem easier to use.

Application scenarios:When you want to provide a simple interface for a complex subsystem. Subsystems tend to become more and more complex as they evolve. In most modes, More and smaller classes are generated. This makes subsystems more reusable and easier to customize, but it also brings some difficulties for users who do not need to customize subsystems. Facade can provide a simple default view, which is sufficient for most users, and users who need more customization can bypass the facade layer. There is a large dependency between the implementation part of the client program and the abstract class. Introducing facade to separate this subsystem from customers and other subsystems can improve the independence and portability of the subsystem. When you need to build a layered sub-system, use the facade Module
Defines the entry points for each layer of the subsystem. If subsystems are mutually dependent, you can make them communicate only through facade, thus simplifying the dependency between them.

 

7 proxy Mode

Core Ideas:Logically inherit a subclass of an existing class to extend the functions of the original subclass.

Application scenarios:This decouples the logic and implementation. Use proxy as the implementation. The left and right logic of concretesubject.

 

--If not, you are welcome to criticize and advise.This is the beginning of the structural model and will be detailed in the future.

 

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.