Adaptation mode

Architectural design classes, when designing interfaces, you need to consider using design patterns (especially when using external interfaces)

Adaptation, the same strategy effect provides a unified entry

Adaptation mode: The method names required by different users are different from the interface method names we provide. If you want to use this, you need an adaptation class, and the adaptation target method name needs to be covered by a layer (the interface method is called inside) (the conversion method name)

Since the client does not matter which interface you come from, it cannot be injected through construction, using spring injection, etc.

 

 

 

1 Overview

  Convert the interface of a class to another interface that the client wants. The Adapter pattern enables classes to work together that would otherwise not work together due to incompatible interfaces.

2. Problem solved

  That is, the Adapter pattern enables classes that would otherwise not work together due to incompatible interfaces to work together.

3. Roles in the pattern

  3.1 Target interface (Target): The interface expected by the client. Targets can be concrete or abstract classes, or interfaces.

  3.2 The class that needs to be adapted (Adaptee): the class or adaptor class that needs to be adapted.

  3.3 Adapter: Convert the original interface to the target interface by wrapping an object that needs to be adapted.  

4. Mode Interpretation

  Note: In the design pattern of GoF, there are two types of adapter patterns, class adapter pattern and object adapter pattern . Since the class adapter pattern matches one interface with another interface through multiple inheritance, and languages ​​such as C# and java do not support multiple inheritance, only the object adapter is introduced here.

  4.1 Class Diagram of Adapter Pattern

  

  4.2 Code Implementation of Adapter Pattern

copy code
    ///  <summary> 
    /// Define the interface expected by the client
     ///  </summary> 
    public  class Target
    {
        ///  <summary> 
        /// Decorate with virtual so that subclasses can override
         ///  </summary> 
        public  virtual  void Request()
        {
            Console.WriteLine("This is a common request");
        }
    }

    ///  <summary> 
    /// Define the class that needs to be adapted
     ///  </summary> 
    public  class Adaptee
    {
        public void SpecificRequest()
        {
            Console.WriteLine("This is a special request.");
        }
    }

    ///  <summary> 
    /// Define Adapter
     ///  </summary> 
    public  class Adapter:Target
    {
        // Create a private Adeptee object 
        private Adaptee adaptee = new Adaptee();

        ///  <summary> 
        /// By overriding, calling the Request() method on the surface becomes the actual call to SpecificRequest()
         ///  </summary> 
        public  override  void Request()
        {
            adaptee.SpecificRequest();
        }
    }
copy code

  4.3 Client Code

copy code
    class Program
    {
        static void Main(string[] args)
        {
            // 对客户端来说,调用的就是Target的Request()
            Target target = new Adapter();
            target.Request();

            Console.Read();
        }
    }
copy code

  运行结果

  

5. 模式总结

  5.1 优点

    5.1.1 通过适配器,客户端可以调用同一接口,因而对客户端来说是透明的。这样做更简单、更直接、更紧凑。

    5.1.2 复用了现存的类,解决了现存类和复用环境要求不一致的问题。

    5.1.3 将目标类和适配者类解耦,通过引入一个适配器类重用现有的适配者类,而无需修改原有代码。

    5.1.4 一个对象适配器可以把多个不同的适配者类适配到同一个目标,也就是说,同一个适配器可以把适配者类和它的子类都适配到目标接口。

  5.2 缺点

    对于对象适配器来说,更换适配器的实现过程比较复杂。

  5.3 适用场景

    5.3.1 系统需要使用现有的类,而这些类的接口不符合系统的接口。

    5.3.2 想要建立一个可以重用的类,用于与一些彼此之间没有太大关联的一些类,包括一些可能在将来引进的类一起工作。

    5.3.3 两个类所做的事情相同或相似,但是具有不同接口的时候。

    5.3.4 旧的系统开发的类已经实现了一些功能,但是客户端却只能以另外接口的形式访问,但我们不希望手动更改原有类的时候。

    5.3.5 使用第三方组件,组件接口定义和自己定义的不同,不希望修改自己的接口,但是要使用第三方组件接口的功能。

6. 适配器应用举例

  6.1 使用过ADO.NET的开发人员应该都用过DataAdapter,它就是用作DataSet和数据源之间的适配器。DataAdapter通过映射Fill和Update来提供这一适配器。

  6.2 Mobile phone power adapter

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326311216&siteId=291194637