Adapter (structural mode - adapter mode)

Adaptation:
Convert the original incompatible interface to a compatible interface without changing the original implementation.

Adapter mode
insert image description here
intent:
insert image description here
There are two kinds of adapters: object adapter and class adapter. Object adapter 1 and object
adapter are preferred. Structure: Adaptee is the field of Adapter Example: Scenario: Implement Stack through ArrayList Adapter:Stack (adapter) Adaptee:ArrayList (adapted object)



insert image description here



namespace AdapterMethod
{
    
    
    public interface IStack//客户期望的接口
    {
    
    
        void Push(object item);
        object Pop();
        object Peek();
    }

    //对象适配器
    //将ArrayList适配,改变接口为Stack
    public class Stack : IStack//Adapter,适配对象
    {
    
    
        private ArrayList list;//Adaptee,被适配的对象

        public Stack()
        {
    
    
            list = new ArrayList();
        }

        public void Push(object item)
        {
    
    
            list.Add(item);
        }

        public object Pop()
        {
    
    
            object pop = list[list.Count - 1];
            list.RemoveAt(list.Count-1);
            return pop;
        }

        public object Peek()
        {
    
    
            return list[list.Count - 1];
        }
    }

2. Class Adapter
Structure
insert image description here
Example
Scenario: Implement Stack through ArrayList

 public interface IStack//客户期望的接口
    {
    
    
        void Push(object item);
        object Pop();
        object Peek();
    }
//类适配器
public class Stack01 : ArrayList, IStack
{
    
    
    public void Push(object item)
    {
    
    
        this.Add(item);
    }

    public object Pop()
    {
    
    
        object pop = this[this.Count - 1];
        this.RemoveAt(this.Count - 1);
        return pop;
    }

    public object Peek()
    {
    
    
        return this[this.Count - 1];
    }
}

Why is Object Adapter preferred?
Because the class adapter inherits from the original interface (ArrayList) and the user-desired interface (IStack), this class (Stack01) can be used not only where the original interface is available, but also where the user expects the interface to be available, which violates the single responsibility principle.

Concrete and complete example
1. Existing classes

public class ExitingClass//已存在的类
    {
    
    
        public void SpecificRequst1()
        {
    
    
        }

        public void SpecificRequest2()
        {
    
    
        }
    }

2. Interfaces used in the new environment

    interface ITarget//新环境使用的接口
    {
    
    
        void Requst();
    }

3. Adapter

 class Adapter : ITarget
    {
    
    
        private ExitingClass adaptee;

        ExitingClass Adaptee()
        {
    
    
            return new ExitingClass();
        }

        public void Requst()
        {
    
    
            adaptee.SpecificRequest2();
            adaptee.SpecificRequst1();
        }
    }

4. use

    class  MySystem
    {
    
    
        public void Process(ITarget target)
        {
    
    
        }
    }

main point
insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/weixin_51565051/article/details/131517007