iOS design mode--adapter mode and viewer mode

Source: Internet
Author: User

What is the adapter mode?

Adapter mode, so to speak, is used to connect two different kinds of objects so that they work together with no problem. Sometimes it is also called a wrapper. The idea is quite simple, the adapter implements the behavior of one of the interfaces that the client wants, and it connects to another object that has a completely different interface and behavior. On the other side is the client knows how to use the target interface, on the other side of the client is ignorant of the adapter. The adapter stands between the two, and the main function of the adapter is to pass the behavior of the ligand to the client at the other end of the pipeline.

There are basically two ways to implement an adapter. The first is the integration to fit two interfaces, called Class adapters. Class adapters are implemented through multi-integration. The second is the object adapter, unlike the class adapter, where the object adapter does not inherit the ligand, but combines a reference to it.

Adapter mode: Transforms the interface of one class into another interface that the client wants. The adapter mode makes it possible for those classes that cannot work together because the interface is incompatible.

What is the difference between a class adapter and an object adapter?

Class Adapter: Only for a single specific Adaptee class, the Adaptee is adapted to target, easy to overload the behavior of Adaptee, because it is through a direct subclass of the adaptation; there is only one adapter object that requires no additional pointers to indirectly access adaptee.

Object adapter: Can fit multiple adaptee and its subclasses, it is difficult to overload adaptee behavior, need to use the object of the subclass instead of adaptee itself; additional pointers are needed to indirectly access adaptee and adapt its behavior.

Obviously, the delegate (Delegate) belongs to the object adapter.

When to use adapter mode?

@: An interface with an existing class does not match the requirements.

@: You want a reusable class that can collaborate with other classes that might have incompatible interfaces.

@: Several different subclasses of a class need to be adapted, but it is unrealistic to have each subclass subclass a class adapter. You can then use an object adapter (also called a delegate) to adapt its parent class interface.
The adapter is implemented through a delegate (Delegate) in OC.


What is Observer mode?

In observer mode, any change in the state of an object notifies another object that is interested in the change. These objects do not need to know each other's existence, this is actually a loosely coupled design. When a property changes, we usually use this pattern to notify other objects.

In the common implementation of this pattern, the observer registers a change event for the state of other objects of interest to the viewer. When the state changes, all observers receive notifications. Apple's push notification is an example of this pattern. If you want to follow the concept of the MVC pattern, you need to have the model objects communicate with the view objects without referencing each other directly. This is where the observer pattern comes in.

Cocoa implements the observer pattern by using notifications (nsnotification) and KVO.

Notice and KVO

Notification: A notification is a model based on the subscription-release pattern that enables publishers to send messages to subscribers, and publishers do not need to have any knowledge of the subscription. Notifications are heavily used by Apple officials, and when the keyboard pops up or is hidden, the system sends uikeyboardwillshownotification/uikeyboardwillhidenotification notifications independently. Uiapplicationdidenterbackgroundnotification notifications are sent when your app is running in the background. When you open the UIApplication.h file, you will see a large number of notifications sent by the system at the end of the file.

The KVO mechanism makes it possible for an object to perceive a change in properties, which is especially important in the scenario of the model-view-controller pattern, because it allows the view object to observe the change of the model object through the controller layer.

The main difference between notification and KVO: notification, a central object that provides notification of changes for all observers, mainly focusing on program events in broad sense. KVO, the observed object sends a notification directly to the observer, bound to the value of a particular object property.

When to use Observer mode?

@: There are two kinds of abstract classes that depend on each other. By encapsulating them in their own objects, they can be individually altered and reused.

@: Changes to an object need to change other objects at the same time without knowing exactly how many objects remain to be changed.

@: An object must notify other objects, and it does not need to know what other objects are.

Reference article:

http://blog.jobbole.com/55505/

Demo Address:

Https://github.com/guoshimeihua/MemoryAndCommandDemo


iOS design mode--adapter mode and viewer mode

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.