Structural Pattern of Design Pattern

Source: Internet
Author: User

There are three types of design patterns:

(1) Creation Mode: Factory method mode, abstract factory mode, Singleton mode, builder mode, and prototype mode.

(2) There are seven structural modes: adapter mode, decorator mode, proxy mode, appearance mode, bridging mode, combination mode, and meta mode.

(3) 11 behavior models: rule mode, template method mode, observer mode, iteration sub-mode, responsibility chain mode, command mode, memorandum mode, state mode, visitor mode, intermediary mode, interpreter mode.

 

Adapter Pattern)-- Transform the interface of a class into another interface that the client expects, so that the two classes that the original interface does not match and cannot work together can work together.

Advantages:

  • Better reusability
    The system needs to use existing classes, and such interfaces do not meet the requirements of the system. The adapter mode enables better reuse of these functions.
  • Decoupling
    Decouples the target class and adaptation class, and introduces an adapter class to reuse the existing adapter class without modifying the original code.
  • Compliant with the open-close Principle
    The same adapter can adapt both the adapter class and its subclass to the target interface. Different adapters can be implemented for different target interfaces without modifying the class to be adapted.

Decorator)-- Describe existing classes to expand some functions

Advantages:
  • The decoration class and the decorated class can develop independently without coupling.
  • The decoration mode is an alternative mode of inheritance, but it provides more flexibility than inheritance.
  • The decoration mode dynamically extends the functions of an implementation class. You can select different decorators at runtime to implement different behaviors.

Disadvantages:

  • Multi-layer decoration is complex

Proxy Pattern)-- Indirectly access the target object by introducing a proxy object

Advantages:

  • Coordinates callers and callers to reduce the Coupling Degree of the system.
  • The proxy object acts as an intermediary between the client and the target object, and plays a role in protecting the target object.

Disadvantages:

  • A proxy object is added between the client and the real topic, which slows down request processing;
  • Implementing the proxy mode requires additional work (some proxy mode implementation is very complicated), which increases the complexity of the system implementation.

Facade Pattern)

  1. After the appearance role is introduced, the user only needs to interact with the appearance role;
  2. The complex logical relationship between users and subsystems is implemented by the appearance role.

Advantages:

  • This reduces the coupling between the customer class and the sub-system class, and achieves the loose coupling between the sub-system and the customer class.
  • The appearance mode shields sub-system components from the customer, thus simplifying the interface

Disadvantages:

  • Without introducing abstract appearance classes, adding a new subsystem may need to modify the appearance class or client source code, which violates the "open/closed principle"

Bridge)-- Decouples abstraction and implementation so that the two can change independently

Advantages:

  • Separated abstraction and implementation
  • Better scalability

Disadvantages:

  • The introduction of the bridge mode makes the system more difficult to understand and design. Since the aggregation association is established on the abstraction layer, developers are required to design and program the abstraction.
  • The bridge mode requires correct identification of two independent dimensions in the system, so the scope of use has certain limitations.

Composite)-- By introducing an abstract component object as the parent object of the composite object and the leaf object, the composite object and the leaf object are unified, it is always in the operation of component objects, and does not distinguish whether it is in the operation of the combination object or in the operation of the leaf object

Advantages:

  • Defines a class hierarchy that contains basic objects and composite objects, and unifies composite objects and leaf objects.
  • Simplified client calls

The essence of the combination mode: unify the leaf object and the combination object.

Flyweight Pattern)-- Use the sharing technology to effectively support the reuse of a large number of fine-grained objects

Broadly speaking, the string class defined in the JDK class library is also a typical example of using the metadata mode.

Applicable scenarios:

  • A system has a large number of identical or similar objects. Due to the large use of such objects, memory consumption is high.
  • To use the shared object mode, you need to maintain a shared object pool, which consumes resources. Therefore, you should use the shared object mode when you repeatedly use it.

Advantages:

  • This greatly reduces the number of objects in the memory so that only one copy of the same object or similar objects is saved in the memory;
  • The external status of the shared object mode is relatively independent and does not affect its internal status, so that the shared object can be shared in different environments.

Disadvantages:

  • The metadata mode makes the system more complex and requires separation of internal and external states, which complicate the logic of the program;
  • In order for objects to be shared, the metadata mode needs to externalize the state of the object, while reading the external State leads to a longer running time.

Structural Pattern of Design Pattern

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.