What is the exact difference between Adapter and Proxy patterns?

前端 未结 3 1751
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-01 16:56

As I understood both Adapter and Proxy patterns make two distinct/different classes/objects compatible with each for communication. And bot

3条回答
  •  有刺的猬
    2021-01-01 17:23

    Difference between Adapter pattern and Proxy Pattern

    ADAPTER PATTERN

    1. Indian mobile charger (CLIENT) does not fit in USA switch board (SERVER).
    2. You need to use adapter so that Indian mobile charger (CLIENT) can fit in USA switch board (SERVER).
    3. From point 2, you can understand that the CLIENT contacts adapter directly. Then adapter contacts server.

    PROXY PATTERN

    • In adapter pattern client directly contacts adapter. It does not contact server.
    • In proxy pattern, proxy and server implements the same interface. Client would call the same interface.

    UNDERSTANDING THROUGH CODE

    class client{
        public void main(){
          //proxy pattern
          IServer iserver = new proxy();
          iserver.invoke();
    
          //adapter pattern
          IAdapter iadapter = new adapter();
          iserver.iadapter();
        }
    }
    
    class server implements IServer{
        public void invoke(){}
    }
    
    class proxy implments IServer{
      public void invoke(){}
    }
    
    class adapter implements IAdapter{
      public void invoke(){}
    }
    

    Reference: Difference between Adapter pattern and Proxy Pattern

提交回复
热议问题