IOS design patterns-bridging and ios design patterns --

Source: Internet
Author: User

IOS design patterns-bridging and ios design patterns --

We recommend you refer to the OBJECTIVE-C design mode for learning design patterns.

When to use the Bridge Mode

1. Do not want to form a fixed binding relationship between the abstraction and its implementation (so that the implementation can be switched during runtime)

2. abstraction and its implementation should be able to be independently extended through subclass;

3. Modifying abstract implementations should not affect client code

4. if each implementation requires additional subclass to refine the abstraction, it is necessary to divide them into two parts.

5. share an implementation between multiple objects with different abstract Interfaces


The following code describes the design mode.

First, display the called code

//// ViewController. m // BridgeTest /// Created by qitmac000246 on 15/7/13. // Copyright (c) June 2015 dujia. all rights reserved. // # import "ViewController. h "# import" TouchConsoleController. h "# import" GameBoyEmulator. h "@ interface ViewController () @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. // sub-function implementation GameBoyEmulator * gbe = [[GameBoyEmulator alloc] init]; // the function module calls TouchConsoleController * tcc = [[TouchConsoleController alloc] init]; [tcc setEmulator: gbe]; // specify the function object [tcc setCommand: kConsoleUp]; // Implementation}-(void) didReceiveMemoryWarning {[super didreceivemorywarning]; // Dispose of any resources that can be recreated .} @ end

ConsoleEmulator. h

/// Leleemulator. h // BridgeTest /// Created by qitmac000246 on 15/7/14. // Copyright (c) June 2015 dujia. all rights reserved. // # import <Foundation/Foundation. h> typedef enum {kConsoleUp,} ConsoleType; // abstract parent class of the execution class @ interface ConsoleEmulator: NSObject // Abstract Public method-(void) loadInstructionsForCommand; @ end

ConsoleEmulator. m

#import "ConsoleEmulator.h"@implementation ConsoleEmulator- (void) loadInstructionsForCommand{        }@end
GameBoyEmulator. h

#import "ConsoleEmulator.h"@interface GameBoyEmulator : ConsoleEmulator@end

GameBoyEmulator. m

# Import "GameBoyEmulator. h" @ implementation GameBoyEmulator // sub-execution class-(void) loadInstructionsForCommand {NSLog (@ "% s" ,__ func _);} @ end

ConsoleController. h

# Import <Foundation/Foundation. h> # import "leleemulator. h "@ interface ConsoleController: NSObject // define the abstract parent class of the execution class in the parent class of the controller @ property (nonatomic, strong) ConsoleEmulator * emulator; // Abstract Public call method-(void) setCommand :( ConsoleType) command; @ end

ConsoleController. m

#import "ConsoleController.h"@implementation ConsoleController- (void) setCommand:(ConsoleType)command{    [_emulator loadInstructionsForCommand];}@end

TouchConsoleController. h

# Import "lelecontroller. h" // controller subclass @ interface TouchConsoleController: ConsoleController-(void) up; @ end

TouchConsoleController. m

#import "TouchConsoleController.h"@implementation TouchConsoleController- (void) up{    [super setCommand:kConsoleUp];}@end


Code download: http://pan.baidu.com/s/1hqo2ka4

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.