Analysis of Objective-C design pattern adapter


The pattern of picture recognition

Analysis of Objective-C design pattern adapter

A simple overview of the adapter model is: what you need is in front of you, but you can’t transform it in a short time, so we try to adapt it.

For example, now a mobile phone without electricity and a 110v50hz socket are in front of you, you obviously need to charge the mobile phone, but the mobile phone only needs 5v-1a input. What should we do at this time?

  1. Call the power plant and lower the voltage for me

  2. Find a mobile phone manufacturer, change my mobile phone to 220 V input

  3. Find a power adapter

The result is obvious~

Schema definition

Adapter pattern: converts an interface to another one that the customer wants. Adapter pattern enables classes with incompatible interfaces to work together. Its alias is wrapper. Adapter pattern can be used as either class structure pattern or object structure pattern.

The adapter is used to connect two different kinds of objects to make them work together without any problem. The idea is very simple. The adapter implements the behavior of some kind of interface required by the client. At the same time, it connects to another object with (completely) different interfaces and behaviors. One is the target interface that the client knows how to use, and the other is the adapter that the client knows nothing about. The adapter is in between. The main function of the adapter is to pass the behavior of the Adaptee to the client at the other end of the pipeline.

When to use adapter mode

  • The interface of the existing class does not meet the requirements

  • Want a reusable class that can work with other classes that may have incompatible interfaces.

  • It is necessary to adapt several different subclasses of a class, but it is unrealistic for each subclass to subclass a class adapter. Then you can use an object adapter (that is, a proxy) to adapt the interface of its parent class.

  • Decoupling (what the adapter does is a process of coupling)

Pattern structure

Adapter structure diagram

Analysis of Objective-C design pattern adapter

Comparison between class adapter and object adapter

Class adapters and object adapters are different ways to implement the adapter pattern, but achieve the same goal.

Class Adapter object adapter
Only for a single concrete adapter class, adapt the adapter to the target It can adapt to multiple adaptees and their subclasses
It’s easy to overload the behavior of adapter because it’s adaptation through direct subclassing It is difficult to overload the behavior of the adapter, so the object of the subclass is needed instead of the adapter itself
There is only one adapter object, and no additional pointer is needed to access the adapter indirectly Additional pointers are required to indirectly access the adapter and adapt its behavior

Code examples

Target (this is the interface that customers expect. The goal can beConcrete or abstract classesIt can beInterface)

class Target
    public virtual void Request()
        Console. Writeline ("normal request");

The code of adapter is as follows:

class Adaptee
    public void SepecificRequest()
        Console. Writeline ("special request");

Adapter (converts the source interface into the target interface by wrapping an adapter object inside). The code is as follows:

class Adapter : Target
    private Adaptee adaptee = new Adaptee();
    public override void Request()

The client code is as follows:

static void Main(String[] args)
    Target target = new Adapter();

More complex applications in cocoa

Analysis of Objective-C design pattern adapter

Take the use of uitableview as an example: you want to transform the interface of uitableview into the interface required by the client.

  • What is the client here? It’s actually uitableview.

  • So what is target here? Is a uitableviewdelegate and uitableviewdatasource.

  • The concrete class that implements the protocol (uiviewcontroller) will be an adapter. What are the classes that do not match the framework and need to be adapted? Other classes in the application (uiviewcontroller).

The reason why we say delegation mechanism is mainly adapter pattern is that delegation mechanism can realize the intention of some other design patterns, such as decoration pattern. Sometimes the implementation of delegation pattern is mixed with other design patterns.


There are many adapter modes, such as:

  • It decouples the client from the adapter and makes the client call more concise (only need to call the defined interface method).

But there are also disadvantages

  • In order to adapt to the target interface, there may be a complex implementation process when writing the adapter;

  • There are many more classes for no reason, which makes the structure more complicated.

So in the use of time need to depend on the situation, do not copy blindly use design patterns.

Recommended Today

Looking for frustration 1.0

I believe you have a basic understanding of trust in yesterday’s article. Today we will give a complete introduction to trust. Why choose rust It’s a language that gives everyone the ability to build reliable and efficient software. You can’t write unsafe code here (unsafe block is not in the scope of discussion). Most of […]