Proxy mode, adapter mode, bridge mode

Structural model

Proxy mode

Introduction: Provide a red proxy for an object to control the object's access. That is 客户端通过代理简介地访问该对象, to restrict, enhance or modify some characteristics of the object.
Definition: For some reason 需要给某对象提供一个代理以控制对该对象的访问. At this time, the 访问对象不适合或者不能直接引用对象proxy object acts as an intermediary between the access object and the target object.

advantage:

  • The proxy mode 客户端与目标对象is played in between 一个中介作用和保护目标对象的作用.
  • The proxy object can extend the functionality of the target object.
  • The proxy mode can separate the client from the target object, which reduces the coupling of the system to a certain extent.

Disadvantages:

  • Adding a proxy object to the client and the target object will slow down the request processing speed.
  • Increased system complexity.

Application scenarios:

  • Remote access, this way is usually to 为了隐藏目标对象存在于不同地址空间的事实facilitate client access.
  • Virtual proxy, this method usually 用于创建的目标开销很大时replace the real object with a small proportion of virtual proxy first to eliminate the user’s feeling that the server is slow
  • Security agent, this way is usually 用于控制不同类型 客户对真实对象的访问权限.
  • Intelligent guidance, this way is mainly 用于当调用目标对象时,代理附加一些额外的处理功能.
  • Lazy loading, this way is in order 提高系统的性能,延迟对目标的加载.

The above is a static proxy mode, which has two disadvantages:

  1. There is a one-to-one correspondence between the real theme and the agency theme, and the addition of the real theme must also increase the agency theme.
  2. Before designing an agent, a real agent must exist in advance, which is not flexible enough.

main character:

  • Abstract subject (Subject) class: Declare business methods implemented by real subjects and proxy objects through interfaces or abstract classes.
  • Real Subject (RealSubject) class: It implements the concrete business in the abstract subject. It is the real object represented by the proxy object and the object we ultimately want to refer to.
  • Proxy class: Provides the same interface as the real theme. It contains references to the real theme. It can access or control or extend the functions of the real theme.

The above problems can be solved by using dynamic agency. The structure diagram is as follows: For
Agency model structure diagram
example: Shaoguan "Tianjie Ejiao" company is an agency company of a Wuyuan specialty company, which is realized by agency mode

  • "Wuyuan Specialty Products Company" is the real theme
  • "Tianjie e corner" is an agent, it can call related methods of Wuyuan Company, and can add some additional processing
  • The client indirectly accesses the products of the "Wuyuan Company" through the "Tianjie e Corner" Company
    Insert picture description here

Adapter mode

Definition: to 一个类的接口转换成客户希望的另一个接口enable those classes that could not work together due to interface incompatibility to work together.

advantage:

  1. The client can transparently call the target interface through the adapter;
  2. Programmers do not need to modify the original code and reuse existing adaptor classes
  3. Decoupling the target class and the adaptor class solves the problem of inconsistency between the target class and the adaptor class interface.

Disadvantages: For class adapters, the implementation process of replacing the adapter is more complicated

Applicable scene:

  1. The previously developed system has classes that meet the functional requirements of the new system, but its interface is inconsistent with the interface of the new system
  2. Applicable to components provided by third parties, but the component definition is different from the interface definition required by yourself

The adapter pattern includes the following main roles:

  1. Target interface: The interface expected by the current system business, which can be an abstract class or interface.
  2. Adaptee class: is the component interface in the existing component library that is accessed and adapted.
  3. Adapter class: It is a converter that converts the adapter interface into a target interface by inheriting or referencing the object of the adapter, allowing customers to access the adapter in the format of the target interface.

注意:一个适配器对应一个适配者, The adapter mode is divided into two types : class adapter mode and object adapter mode
. The structure diagram of the class adapter mode is as follows: The structure diagram of the
Class adapter pattern structure diagram
object adapter mode is as follows:
Object adapter structure diagram
两者的区别: 类适配器直接通过适配者类调用 对象适配器通过实现引用适配者类来调用适配者类中的方法 两者在使用上没有太过于严格的区分

Example: Use the object adapter mode to simulate the engine of a new energy vehicle

  • The engines of new energy vehicles include electric energy engines and light energy engines.
  • The client hopes to use a unified engine drive method to drive () the visitor two engines
    Insert picture description here

Bridge mode

Definition: Separate abstraction and reality so that they can change independently. It is 用组合关系代替继承关系realized, thereby reducing the degree of coupling between the two variable dimensions of abstraction and realization.

advantage:

  1. Due to the separation of abstraction and reality, it has strong scalability
  2. Its implementation details are transparent to customers.

Disadvantages: Since the aggregation relationship is established at the abstract layer, developers are required to design and program for abstraction, which increases the difficulty of understanding and designing the system

Applicable scene:

  1. When a class has two independently changing dimensions, and both dimensions need to be expanded.
  2. When a system does not want to use inheritance or the number of system classes increases sharply because of multi-level inheritance.
  3. When a system needs to add more flexibility between the constructed abstract role and the concrete role.

The main role of bridge mode:

  • Abstraction roles: define abstract classes and include a reference to actualized objects.
  • Extended abstraction (Refined Abstraction) role: It is a subclass of the abstract role, which implements the business methods in the parent class, and calls the business methods in the realized role through the composition relationship.
  • Implementation (Implenmentor) role: defines the interface of the implementation role for the extension of the abstract role to call.
  • Concrete Implementor (Concrete Implementor) role: Give the concrete realization of the actual role interface.

The structure diagram of the bridge mode is as follows:
Bridge mode structure diagram
Example: Use the bridge mode to simulate the purchase of women's leather bags

  • There are many types of women's leather bags, which can be divided according to usage, color, etc., and there are multi-dimensional changes.
    Insert picture description here

Note: The contents of the article picking in the "software design patterns (Java version)", Author: Cheng fine column

Guess you like

Origin blog.csdn.net/weixin_44048668/article/details/108862066