iOS design mode-bridging mode

Source: Internet
Author: User

What is bridging mode?

The purpose of bridging mode is to separate the abstract hierarchy from its implementation so that it can be changed independently. The abstraction layer defines the upper-level abstraction interface for use by clients. The implementation layer defines the underlying interface for use by the abstraction layer. A reference to the implementation class is encapsulated in an instance of the abstraction layer, and the bridge is formed. (a certain similarity to the appearance mode).

Bridging mode: Separates the abstract part from its implementation, so that they can all change independently.

When to use bridging mode?

@: Do not want to create a fixed binding relationship between the abstraction and its implementation so that the implementation can be switched at run time.

@: Abstractions and their implementations should be extensible by subclasses independently.

@: Modifying the implementation of an abstraction should not affect client code.

@: If each implementation requires additional subclasses to refine the abstraction, it is necessary to divide them into two parts.

@: You want to share an implementation between multiple objects with different abstract interfaces.

Example Application of bridging mode

For example, there is a television manufacturer, they produce each TV with a remote control, the user can use the remote control channel switching and other operations. Here the remote control is the interface of the TV, if each TV model needs a dedicated remote control, then the remote control will cause a surge in design. However, each remote control has some features that are common to various models of TVs, such as switching channels, adjusting the volume, and the power switch. And each TV should be able to respond to these commands from the remote control via the basic command interface. We can separate the remote logic from the actual TV model. So the change of the TV model will not have any influence on the design of the remote control. The same design of the remote control can be reused and extended without affecting other TV models. As shown in the following:


Abstractremotecontrol is the parent interface that defines the upper-level interface for use by clients. It has a reference to Tvprotocol vision, and Tvprotocol defines the interface of the implementation class. This interface does not have to be consistent with the Abstractremotecontrol interface, in fact, two interfaces can be completely different. The Tvprotocol interface provides basic operations, while Abstractremotecontrol's upper-level operations are based on these basic operations. When the client sends an operation message to an instance of Abstractremotecontrol, this method sends a OPERATIONIMP message to the IMP, and the underlying actual response is by TVA or TVB and accepts the task.

So when you want to add a new tvprotocol to the system, all you have to do is create a new implementation class for Tvprotocol, respond to the OPERATIONIMP message, and perform any specific actions in it. However, this will not have any effect on the abstractremotecontrol aspect. Similarly, if you want to modify the Abstractremotecontrol interface or create a more granular Abstractremotecontrol class, it will not affect the other end of the bridge.

To see the specific code implementation, first look at the abstract part of the code implementation, the Abstractremotecontrol code is as follows:

#import <Foundation/Foundation.h> #import "TVProtocol.h" @interface Abstractremotecontrol:nsobject@property ( Nonatomic, weak) id<tvprotocol> Tvprotocol; -(void) detecttvfunction; @end
#import "AbstractRemoteControl.h" @implementation abstractremotecontrol-(void) detecttvfunction {NSLog (@) detects the function of the TV , to be implemented by subclasses);} @end

The Abstractremotecontrol class maintains a reference to the Tvprotocol instance object, defines the upper-level abstraction interface detecttvfunction for use by the client, and the implementation of this method is implemented by its subclasses. The Concreteremotecontrol code is as follows:

#import "AbstractRemoteControl.h" @interface concreteremotecontrol:abstractremotecontrol//Override this method-(void) detecttvfunction; @end
#import "ConcreteRemoteControl.h" @implementation concreteremotecontrol-(void) detecttvfunction {[Self.tvprotocol    Switchchannel];    [Self.tvprotocol Adjustvolume]; [Self.tvprotocol Powerswitch];} @end

From here we can see that when the client sends a detecttvfunction message to an instance of Concreteremotecontrol, this method sends Tvprotocol Switchchannel, Adjustvolume, Powerswitch three messages, TVA or TVB will respond and accept the task. At this point, the abstract part of the code has been completed, and then see the implementation of part of the code, Tvprotocol code as follows:

#import <Foundation/Foundation.h> @protocol tvprotocol <NSObject> @required-(void) Switchchannel;  Toggle Channel-(void) adjustvolume;   Adjust Volume-(void) Powerswitch; Power Switch @end

This is a protocol in which three methods are defined, and in the future when the TV instance is created, the protocol must be followed to ensure that the TV has the same function. The code for ABSTRACTTV is as follows:

#import <Foundation/Foundation.h> #import "TVProtocol.h" @interface abstracttv:nsobject <TVProtocol> @end
#import "AbstractTV.h" @implementation abstracttv-(void) Switchchannel {NSLog (@ "Toggle channel, implemented by a specific subclass);} -(void) Adjustvolume {NSLog (@ "Adjusts the volume, implemented by a specific subclass");} -(void) Powerswitch {NSLog (@ "power switch, implemented by a specific subclass");} @end

the code for TVA is as follows:

#import "AbstractTV.h" @interface tva:abstracttv//Override these three methods-(void) switchchannel;-(void) adjustvolume;-(void) Powerswitch; @end
#import "TVA.h" @implementation tva-(void) Switchchannel {NSLog (@ "TV A has the ability to switch channels");} -(void) Adjustvolume {NSLog (@ "TV A has the ability to adjust the volume");} -(void) Powerswitch {NSLog (@ "TV A has the function of the power switch");} @end

TVB's code is as follows:

#import "AbstractTV.h" @interface tvb:abstracttv//Override these three methods-(void) switchchannel;-(void) adjustvolume;-(void) Powerswitch; @end
#import "TVB.h" @implementation tvb-(void) Switchchannel {NSLog (@ "TV B has the ability to switch channels");} -(void) Adjustvolume {NSLog (@ "TV B has the ability to adjust the volume");} -(void) Powerswitch {NSLog (@ "TV B has the function of power switch");} @end

here, the bridge mode code has been completed, how to apply it on the client? We use the following client code to illustrate, as follows:

#import   "ViewController.h" #import   "AbstractRemoteControl.h" #import   "ConcreteRemoteControl.h" # import  "TVProtocol.h" #import   "AbstractTV.h" #import   "TVA.h" #import   "TVB.h" Typedef id <tvprotocol> tvprotocol; //here to make a conversion declaration, otherwise the class does not recognize Tvprotocol. @interface  viewcontroller   () @end @implementation viewcontroller-  (void) viewdidload {    [super  viewdidload];    abstractremotecontrol *remotecontrol = [[ concreteremotecontrol alloc] init];    tvprotocol tvprotocol = [[ tva alloc] init];    remotecontrol.tvprotocol = tvprotocol;         [remoteControl detectTVFunction];         nslog (@ "///////////////////////////////");         Tvprotocol = [[tvb alloc] init];    remotecontrol.tvprotocol = tvprotocol;    [ remotecontrol detecttvfunction];        /**      *   Bridging mode: Separates the abstract part from its implementation, so that they can all change independently.      *   in this example, Abstractremotecontrol is the abstract part, and Tvprotocol is its implementation part.      */    }-  (void) didreceivememorywarning {     [super didReceiveMemoryWarning];    // Dispose of  Any resources that can be recreated.} @end

The log output is as follows:

2015-09-01 22:59:06.295 bridge[16464:703747] TV A has the ability to switch channels 2015-09-01 22:59:06.295 bridge[16464:703747] TV A With the function of adjusting the volume 2015-09-01 22:59:06.296 bridge[16464:703747] TV A has the function of the power switch 2015-09-01 22:59:06.296 bridge[16464:703747]// 2015-09-01 22:59:06.296 bridge[16464:703747] TV B has the function of switching channels 2015-09-01 22:59:06.296 BRIDGE[16464:703747] TV B has the function of adjusting the volume 2015-09-01 22:59:06.296 bridge[16464:703747] TV B has the function of power switch

Through the application of bridging mode, we can separate the abstract part from the implementation part so that all of them can change independently. For example, in this case, the modification of Abstractremotecontrol does not affect the tvprotocol. The same changes to Tvprotocol will not affect Abstractremotecontrol. This is the convenience of bridging mode.

Demo Link Address: Https://github.com/guoshimeihua/Bridge

iOS design mode-bridging 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.