Explain the use of the adapter pattern in the design pattern in objective-c programming _ios

Source: Internet
Author: User
Tags inheritance

Introduction

In project development, there are times when you need to use "some existing objects" that were previously developed, but the interfaces required in the new environment are not satisfied by these existing objects. How do you respond to the need for such migrations? This makes it possible to reuse these objects to meet the new application environment, which is the problem that the adapter (Adapter) solves.

Defined

"Converts the interface of a class into another interface that the customer expects. Adapter mode makes it possible for those classes that cannot work together because of incompatible interfaces. ”

The initial definition appears in the design pattern (addison-wesley,1994).
This definition should be well understood, life is full of the application of adapter mode, such as our mobile phone charger: Mobile phone can not be directly charged in the 220V power supply, the charger will be converted to the voltage of the mobile phone to the required voltage, the phone can be normal charging, the charger has played an appropriate role.

Structure diagram

There are two ways to implement the adapter pattern. The first is to fit two interfaces through inheritance, which is called a class adapter. In the book Gof introduces design Patterns, class adapters are implemented through multiple inheritance. The language used in the book is C + +, and it does not have the syntax of C # Interface or OC protocol, everything is a class. In OC, a class can implement a protocol while inheriting the parent class to achieve the effect of multiple C + + inheritance. To implement a class adapter in OC, you first need to have a protocol that defines the set of behaviors that the client wants to use, and then implement the Protocol with a specific adapter class. The adapter class also inherits the fittest. The class adapter structure diagram looks like this:

As you can see from the diagram, adapter is a target type and also a adaptee type. It overloads the target request method, does not overload the Specificrequest method in Adaptee, but invokes the Specificrequest method of the parent class in the implementation of the adapter request method. A class adapter can be implemented only if target is a protocol rather than a class, because there is no multiple inheritance in the OC.
The second way to implement the adapter pattern is called the object adapter. Unlike a class adapter, the object adapter does not inherit the adaptor, but instead combines a reference to it. The object adapter structure diagram looks like this:

As you can see from both charts, the relationship between target and adapter is the same, and the relationship between adapter and Adaptee is changed from inheritance to association. In this relationship, adapter needs to keep a reference to Adaptee. In the request method, adapter sends a [_adaptee specificrequest] message to Adaptee to complete the client's request.

Obviously, the commonly used delegate (Delegate) pattern in OC belongs to the object adapter. As an example of a commonly used uitableviewdelegate, I'll draw its structural chart here, as follows:

UITableView (the client role in the object adapter) handles the selected row event, the message is passed to Uitableviewdelegate (the target role in the object adapter), and then the Myviewcontroller is invoked ( Adapter role in Object adapters-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) Indexpath method to process, in Myviewcontroller this method, we will invoke other objects (for example, we may invoke the details Detailviewcontroller object to jump to the details page) to process the message.

Instance
Target class Interface

Copy Code code as follows:

#import <Foundation/Foundation.h>

@interface Target:nsobject
-(void) Request;
@end


Target class implementation
Copy Code code as follows:

#import "Target.h"

@implementation Target

-(void) request{
NSLog (@ "Ordinary request!) ");
}
@end


Adaptee class Interface
Copy Code code as follows:

#import <Foundation/Foundation.h>

@interface Adaptee:nsobject
-(void) specificrequest;
@end


Adaptee class implementation
Copy Code code as follows:

#import "Adaptee.h"

@implementation Adaptee

-(void) specificrequest{
NSLog (@ "Special request!) ");
}
@end


Adapter class Interface
Copy Code code as follows:

#import "Target.h"

@class adaptee;
@interface adapter:target{
Adaptee *adaptee;
}
@end


Adapter class implementation
Copy Code code as follows:

#import "Adapter.h"
#import "Adaptee.h"

@implementation Adapter

-(ID) init{
if (self = = [Super init]) {
Adaptee = [[Adaptee alloc]init];
}
return self;
}
-(void) request{
[Adaptee Specificrequest];
}
@end


Main method call
Copy Code code as follows:

#import <Foundation/Foundation.h>
#import "Adapter.h"

int main (int argc,const char *argv[])
{
@autoreleasepool {
Target *target = [[Adapter alloc]init];
[Target Request];
}
return 0;
}

Summary

1. Adapter mode is mainly used to "want to reuse some existing classes, but the interface is inconsistent with the requirements of the reuse environment", in the legacy code reuse, class library migration is very useful.

2. Adapter mode has the object adapter and class adapter in two forms of implementation structure, but the class adapter in the "Multiple inheritance" implementation, resulting in poor high coupling, so generally not recommended, in addition, the OC does not support multiple inheritance. Object adapters take the form of "object composition" and are more compliant with loosely coupled specifications.

You can consider using adapter mode in the following situations:

1. A class that already exists needs to be used, and its interface does not conform to the specification of the new environment.
2. You want to create a reusable class that works in conjunction with other unrelated classes or unforeseeable classes, which are classes that might not necessarily be compatible with the interfaces.

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.