美文网首页
iOS设计模式(二)桥接模式

iOS设计模式(二)桥接模式

作者: handsome5 | 来源:发表于2018-05-12 18:24 被阅读28次
桥接模式
  • 1.定义
    将抽象部分与实现部分分离,使它们都可以独立的变化.
  • 2.原理
    抽象层与实现层相分离,抽象层定义了供客户端调用的抽象接口,实现层提供了具体的逻辑,实现类的引用被封装到抽象层的实例中,桥接就形成.
桥接模式UML图 桥接模式UML.png
demo演示 - 桥接模式
  • Remote抽象类
#import <Foundation/Foundation.h>
#import "TV.h"

@interface Remote : NSObject

@property (nonatomic,strong)TV *tv;

-(void)setCommand:(NSString *)command;

@end

#import "Remote.h"

@implementation Remote

- (void)setCommand:(NSString *)command
{
    //桥接的形成
    [self.tv loadCommand:command];
}
@end
  • SubRemote类,继承Remote类
#import "Remote.h"

@interface SubRemote : Remote

- (void)up;
- (void)down;
- (void)left;
- (void)right;
@end

#import "SubRemote.h"

@implementation SubRemote

- (void)up
{
    [super setCommand:@"up"];
}
- (void)down
{
    [super setCommand:@"down"];
}
- (void)left
{
     [super setCommand:@"left"];
}
- (void)right
{
  [super setCommand:@"right"];
}
@end
  • TV类,抽象类
#import <Foundation/Foundation.h>

@interface TV : NSObject

- (void)loadCommand:(NSString *)command;

@end

#import "TV.h"

@implementation TV

//抽象方法
- (void)loadCommand:(NSString *)command
{
//无需实现    
}
@end
  • XiaoMiTV类,继承TV
#import "TV.h"
@interface XiaoMiTV : TV
@end

#import "XiaoMiTV.h"

@implementation XiaoMiTV

- (void)loadCommand:(NSString *)command
{
    if ([command isEqualToString:@"up"]) {
        NSLog(@"你按的是xiaoMi====:%@",command);
    }
    else if ([command isEqualToString:@"down"]) {
        NSLog(@"您按的是XM:----%@",command);
    }
    else if ([command isEqualToString:@"left"]) {
        NSLog(@"您按的是XM:----%@",command);
    }
    else if ([command isEqualToString:@"right"]) {
        NSLog(@"您按的是XM:----%@",command);
    }
    else {
        NSLog(@"超出范围");
    }
}
@end
  • HaiXinTV类,继承TV
#import "TV.h"

@interface HaiXinTV : TV

@end

#import "HaiXinTV.h"

@implementation HaiXinTV

- (void)loadCommand:(NSString *)command
{
        if ([command isEqualToString:@"up"]) {
            NSLog(@"你按的是HaiXin====:%@",command);
        }
        else if ([command isEqualToString:@"down"]) {
            NSLog(@"您按的是HaiXin:----%@",command);
        }
        else if ([command isEqualToString:@"left"]) {
            NSLog(@"您按的是HaiXin:----%@",command);
        }
        else if ([command isEqualToString:@"right"]) {
            NSLog(@"您按的是HaiXin:----%@",command);
        }
        else {
            NSLog(@"超出范围");
        }
}
@end
  • VC类
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
@end

#import "ViewController.h"
#import "SubRemote.h"
#import "XiaoMiTV.h"
#import "HaiXinTV.h"

@interface ViewController ()
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    SubRemote *subRemote = [[SubRemote alloc] init];
    XiaoMiTV *xmTV = [[XiaoMiTV alloc] init];
    subRemote.tv = xmTV;
    [subRemote up];
    [subRemote down];
    
    HaiXinTV *hxTV = [[HaiXinTV alloc] init];
    subRemote.tv = hxTV;
    [subRemote up];
    [subRemote down];
}
@end
测试结果 测试.png
小结
  • 桥接模式的特点:
    1.将依赖具体的实现,该为依赖抽象,起到一个松耦合的作用.
    2.分离了接口实现部分.
    3.提高扩展性.
demo地址:https://github.com/defuliu/BrigePatterns.git

相关文章

  • iOS设计模式(二)桥接模式

    桥接模式 1.定义将抽象部分与实现部分分离,使它们都可以独立的变化.2.原理抽象层与实现层相分离,抽象层定义了供客...

  • 设计模式-桥接模式

    设计模式-桥接模式 定义 桥接模式(Bridge Pattern)也称为桥梁模式、接口(Interface)模式或...

  • iOS设计模式-桥接模式

    问题: 假如:我们需要大中小3种型号的画笔,能够绘制12种不同的颜色,如果使用蜡笔,需要准备3x12=36支,但如...

  • 桥接模式

    设计模式:桥接模式(Bridge)

  • 设计模式——桥接模式

    设计模式——桥接模式 最近公司组件分享设计模式,然而分配给我的是桥接模式。就在这里记录我对桥接模式的理解吧。 定义...

  • 设计模式之桥接模式

    设计模式之桥接模式 1. 模式定义 桥接模式又称柄体模式或接口模式,它是一种结构性模式。桥接模式将抽象部分与实现部...

  • iOS设计模式三(中介者,观察者)

    承接上文iOS设计模式二(适配器,桥接,外观)本文为去耦合--获取源码 目录1.中介者模式2.观察者模式 1.中介...

  • iOS设计模式之桥接模式

    桥接模式 1、什么是桥接模式 将抽象部分与实现部分分离,使它们都可以独立的变化。 桥接模式一共有两个角色: 抽象角...

  • 桥接模式

    介绍 桥接模式(Bridge Pattern) 也称为桥梁模式,是结构型设计模式之一。桥接模式的作用就是连接 "两...

  • Java设计模式——桥接模式

    Java设计模式之桥接模式 回顾 上一期分享了适配器模式,主要为了实现解耦 桥接模式 简介 桥接模式是对象的结构模...

网友评论

      本文标题:iOS设计模式(二)桥接模式

      本文链接:https://www.haomeiwen.com/subject/phdacftx.html