IOS design mode Learning (8) Adapter

Source: Internet
Author: User

1 Preface
In object-oriented software design, sometimes we want to apply useful and well-tested classes to other new areas of the application. However, new functions require new interfaces, which are inconsistent with existing classes to be reused. We do not want to rewrite reliable classes for new interfaces.

The incompatibility between existing classes and new interfaces is quite common and people have found a solution for it. This solution is widely used and eventually incorporated into the design mode to become an adapter.

2. Details
2.1 Brief Introduction
The adapter is used to connect two different types of objects so that they can work together without any problem. Sometimes it is also called the wrapper ). The main function of the adapter is to pass the behavior of the adapter to the client at the other end of the pipeline.


Adapter mode: converts an interface of a class to another interface that the customer wants. The adapter mode makes it possible for classes that cannot work together due to interface incompatibility.

2.2 categories
(1) The first method is to adapt two interfaces to become class adapters through inheritance. To implement the class adapter in Objective-C, you must first define a set of behavior protocols to be used by the client, and then use a specific adapter to implement this protocol. The adapter class also inherits the adapter.

(2) The second is the Object Adapter. The Object Adapter does not inherit the adapter, but combines a reference to it. The relationship between the adapter and the adapter changes from "belonging" to "include ". In this relationship, the adapter maintains a reference to the adapter.

2.3 differences between class adapters and object adapters
(1) the class adapter only applies to a single specific adapter and matches the adapter with the target interface. The Object Adapter can adapt to multiple adaptors and Their subclasses.

(2) the class adapter is easy to reload the behavior of the adapter, because it is adapted through direct subclass; the Object Adapter is difficult to reload the behavior of the adapter, you need to use the object with the subclass instead of the adapter itself.

(3) the class adapter has only one adapter object, and no extra pointer is required to indirectly access the adapter. Additional pointers are required to indirectly access the adapter and adapt to its behavior.

2.4 When to use the adapter Mode
(1) existing interfaces and requirements do not match;

(2) A reusable class that can collaborate with other classes that may have incompatible interfaces;

(3) it is necessary to adapt to several different sub-classes of a class, but it is unrealistic to make each sub-class de-subclass A Class adapter.

2.5 relationship between delegation and adapter
Adapter mode: Convert the class interface to the interface required by the client. What are the clients here? Is a class in the Cocoa Touch framework. So what is the Target interface? Is a Commission protocol. The specific class implementing the Protocol will be an adapter. So what is the class that does not match the framework that needs to be adapted? Other classes in the application. Now I understand why the delegate mode is actually the adapter mode.

The reason we say that the delegated mode is mainly the adapter mode because the delegated mechanism can realize the intention of some other design modes, such as the decorative mode. The implementation of the delegated mode is sometimes mixed with other design modes.

3 conclusion
The above is all content and I hope it will help you.

 

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.