iOS common design mode-Adapter Adapter

Source: Internet
Author: User

1. What is adapter design mode (Adapter) (what)

The adapter design pattern is a structured design pattern that transforms a class's interface into another interface that the customer wants, so that those classes that would otherwise not work together because of incompatible interfaces can work together.

Adapter design mode is implemented in two ways: 1.) through inheritance to implement two interfaces, called Class adapters, 2.) by reference to avoid object adapters inheriting the object, called the object adapter.


Figure 1: UML diagram for class adapters

In the class adapter, the relationship between adapter and adaptee is inheritance, and we implement overrides for The + Specificreque () method in Adaptee by inheritance.


Figure 2: Object Adapter UML diagram



In the object adapter, adapter provides a reference to adaptee that invokes the + Specificreque () method in Adaptee.

In iOS, the common delegate pattern belongs to the object adapter, taking the UML diagram of Uitableviewdelegate as an example:



Figure 3: UML diagram for UITableView


UITableView (object in the adapter.Clientrole)when a selected row event is processed, the message is passed to theUitableviewdelegate (in the object adapterTargetrole), and then callMyviewcontroller (in the object adapterAdapterrole)inside the-(void) TableView: (uitableview*) TableView Didselectrowatindexpath: (Nsindexpath *) Indexpathmethod is used to process theMyviewcontrollerin this method, we will call other objects(For example, we might invoke the detailsDetailviewcontrollerobject to jump to the details page)to process the message

2. When to use adapter design mode (when)

when the programmer wants to reuse some existing classes, but the interface is inconsistent with the requirements of the reuse environment ", Legacy code reuse, class library migration and other aspects are very useful.

Adapter mode has two forms of object adapter and class adapter implementation structure, but the class adapter with "Multi-inheritance" implementation, resulting in poor high coupling, it is generally not recommended, in addition, OC multiple inheritance is also not supported in. Object adapters are implemented in a "combination of objects" manner, more in line with the loosely coupled specification.

3. How to use adapter design mode in a project

1.) Create a command-line program, a Adaptee class that inherits from NSObject, which has a method-(void) dosomething, and we assume that the client is the main function, the main function cannot call the method directly, This is what we need to create a adapter class and define a target protocol that defines an interface-(void) usepublicinterface;

2.) Let adapter inherit the target protocol and associate it with Adaptee, then implement the interface defined in the protocol and invoke the method in the Adaptee class in the protocol.

3.) calls to the Adaptee-(void) DoSomething method can be implemented at the client by calling the Usepublicinterface method through adapter.

In fact, as long as the implementation of UITableView friends, are consciously or unconsciously using the adapter design mode, specific in the previous article has been described in detail.


*************************************************************************************************************** *****************************************

—————— presented by Lota Grapefruit






iOS common design mode-Adapter Adapter

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.