23种设计模式(九)

版本记录

版本号 时间
V1.0 2017.04.29

前言

前面讲了23种设计模式中的前几个,下面我们继续,先看前几篇文章。
1. 23种设计模式(一)
2. 23种设计模式(二)
3. 23种设计模式(三)
4. 23种设计模式(四)
5. 23种设计模式(五)
6. 23种设计模式(六)
7. 23种设计模式(七)
8. 23种设计模式(八)

详述

十七、桥接模式——Bridge

  将抽象部分与它的实现部分分离,使它们都可以独立地变化。先看一张原理图。

23种设计模式(九)_第1张图片
桥接模式原理图

在看一下写的代码结构图。

23种设计模式(九)_第2张图片
代码结构原理图

下面看代码。


1. ViewController.m
#import "ViewController.h"
#import "HCDPhone.h"
#import "HCDNokiaPhone.h"
#import "HCDSonyPhone.h"
#import "HCDSoftware.h"
#import "HCDGameSoftware.h"
#import "HCDGameNokiaSoftware.h"
#import "HCDAddressListSofeware.h"

typedef id HCDSoftware;

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    HCDPhone *phone = [[HCDNokiaPhone alloc]init];
    HCDSoftware software = [[HCDGameNokiaSoftware alloc]init];
    [phone setSoftWare:software];
    [phone playSoftPhone];
    software = [[HCDAddressListSofeware alloc]init];
    [phone setSoftWare:software];
    [phone playSoftPhone];
    
}

@end


2. HCDPhone.h

#import 
#import "HCDSoftware.h"

@interface HCDPhone : NSObject

@property (nonatomic, strong) id softWare;

- (void)playSoftPhone;

@end

3. HCDPhone.m

#import "HCDPhone.h"

@implementation HCDPhone

- (void)playSoftPhone
{
    [self.softWare run];
}

@end


4. HCDNokiaPhone.h

#import "HCDPhone.h"

@interface HCDNokiaPhone : HCDPhone

@end


5. HCDNokiaPhone.m
#import "HCDNokiaPhone.h"

@implementation HCDNokiaPhone

@end


6. HCDSonyPhone.h

#import "HCDPhone.h"

@interface HCDSonyPhone : HCDPhone

@end


7. HCDSonyPhone.m

#import "HCDSonyPhone.h"

@implementation HCDSonyPhone

@end

8. HCDSoftware.h

#import 

@protocol HCDSoftware 

- (void)run;

@end


9. HCDGameSoftware.h
#import 
#import "HCDSoftware.h"

@interface HCDGameSoftware : NSObject 

@end



10. HCDGameSoftware.m
#import "HCDGameSoftware.h"

@implementation HCDGameSoftware

- (void)run
{
    NSLog(@"运行游戏手机软件");
}

@end

11. HCDAddressListSofeware.h

#import 
#import "HCDSoftware.h"

@interface HCDAddressListSofeware : NSObject 

@end


12. HCDAddressListSofeware.m
#import "HCDAddressListSofeware.h"

@implementation HCDAddressListSofeware

- (void)run
{
    NSLog(@"运行通讯录手机软件");
}

@end


13. HCDGameNokiaSoftware.h

#import "HCDGameSoftware.h"

@interface HCDGameNokiaSoftware : HCDGameSoftware

@end


14. HCDGameNokiaSoftware.m

#import "HCDGameNokiaSoftware.h"

@implementation HCDGameNokiaSoftware

- (void)run
{
    NSLog(@"运行Nokia的游戏软件");
}

@end


看结果。

2017-04-29 00:24:34.060 17桥接模式[2452:86743] 运行Nokia的游戏软件
2017-04-29 00:24:34.060 17桥接模式[2452:86743] 运行通讯录手机软件

结论:思想。


十八、命令模式——Command

  将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化,对请求排队或记录请求日志,以及支持可撤销的操作。
先看一下代码结构图。

23种设计模式(九)_第3张图片
代码结构图

下面看代码。


1. ViewController.m
#import "ViewController.h"
#import "HCDWaiter.h"
#import "HCDCustomr.h"
#import "HCDWorker.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    HCDWaiter *waiter = [[HCDWaiter alloc]init];
    HCDCustomr *customer = [[HCDCustomr alloc]init];
    [waiter addOrder:[customer pushOrderWithString:@"十串羊肉" type:YES]];
    HCDCustomr *customer1 = [[HCDCustomr alloc]init];
    [waiter addOrder:[customer1 pushOrderWithString:@"二十串鸡肉" type:NO]];
    [waiter notifyOrder];
    
}


@end

2. HCDOrder.h

#import 
#import "HCDOrder.h"
#import "HCDWorker.h"

@interface HCDOrder : NSObject

@property (nonatomic, copy) NSString *orderString;

- (instancetype)initWithOrderString:(NSString *)orderString;

- (void)executeOrder;

@end

3. HCDOrder.m

#import "HCDOrder.h"

@implementation HCDOrder

- (instancetype)initWithOrderString:(NSString *)orderString
{
    self = [super init];
    if (self) {
        _orderString = orderString;
    }
    return self;
}

@end


4. HCDMuttonOrder.h

#import "HCDOrder.h"

@interface HCDMuttonOrder : HCDOrder

@end

5. HCDMuttonOrder.m

#import "HCDMuttonOrder.h"

@implementation HCDMuttonOrder

- (void)executeOrder
{
    [[HCDWorker sharedWorker] doMuttonWork:self.orderString];
}

@end

6. HCDChickenOrder.h

#import "HCDOrder.h"

@interface HCDChickenOrder : HCDOrder

@end

7. HCDChickenOrder.m
#import "HCDChickenOrder.h"

@implementation HCDChickenOrder

- (void)executeOrder
{
    [[HCDWorker sharedWorker] doChickenWork:self.orderString];
}

@end

8. HCDWorker.h
#import 

@interface HCDWorker : NSObject

+ (instancetype)sharedWorker;

- (void)doMuttonWork:(NSString *)work;

- (void)doChickenWork:(NSString *)work;

@end


9. HCDWorker.m


#import "HCDWorker.h"

@implementation HCDWorker

+ (instancetype)sharedWorker
{
    static dispatch_once_t once;
    static HCDWorker *sharedWorker = nil;
    dispatch_once(&once, ^{
        sharedWorker = [[HCDWorker alloc]init];
    });
    return sharedWorker;
}

- (void)doMuttonWork:(NSString *)work
{
    NSLog(@"厨师考羊肉:%@",work);
}

- (void)doChickenWork:(NSString *)work
{
    NSLog(@"厨师考鸡肉:%@",work);
}

@end

10. HCDWaiter.h
#import 
#import "HCDOrder.h"
#import "HCDMuttonOrder.h"
#import "HCDChickenOrder.h"

@interface HCDWaiter : NSObject

- (void)addOrder:(HCDOrder *)order;

- (void)deleteOrder:(HCDOrder *)order;

- (void)notifyOrder;

@end


11. HCDWaiter.m
#import "HCDWaiter.h"

@interface HCDWaiter ()

@property (nonatomic, strong) NSMutableArray *orderList;

@end

@implementation HCDWaiter

- (instancetype)init
{
    self = [super init];
    if (self) {
        _orderList = [NSMutableArray array];
    }
    return self;
}

- (void)addOrder:(HCDOrder *)order
{
    [self.orderList addObject:order];
}

- (void)deleteOrder:(HCDOrder *)order
{
    [self.orderList removeObject:order];
}

- (void)notifyOrder
{
    for (HCDOrder *order in self.orderList) {
        [order executeOrder];
    }
}

@end

12. HCDCustomr.h

#import 

@class HCDOrder,HCDMuttonOrder,HCDChickenOrder;

@interface HCDCustomr : NSObject

- (HCDOrder *)pushOrderWithString:(NSString *)string type:(BOOL)type;

@end


13. HCDCustomr.m
#import "HCDCustomr.h"
#import "HCDMuttonOrder.h"
#import "HCDChickenOrder.h"

@implementation HCDCustomr

- (HCDOrder *)pushOrderWithString:(NSString *)string type:(BOOL)type
{
    HCDOrder *order = nil;
    if (type) {
       order = [[HCDMuttonOrder alloc]initWithOrderString:string];
    }else{
       order = [[HCDChickenOrder alloc]initWithOrderString:string];
    }
    return order;
}

@end

下面直接看结果。

2017-04-29 01:08:20.581 18命令模式[2961:109594] 厨师考羊肉:十串羊肉
2017-04-29 01:08:20.581 18命令模式[2961:109594] 厨师考鸡肉:二十串鸡肉

结论:注意思想。

后记

  今晚就写这么多了,祝大家也祝我自己五一快乐!未完,待续!谢谢,各位晚安!!

23种设计模式(九)_第4张图片
风景欣赏

你可能感兴趣的:(23种设计模式(九))