23种设计模式(九)
2017-04-29 本文已影响25人
刀客传奇
版本记录
版本号 | 时间 |
---|---|
V1.0 | 2017.04.29 |
前言
前面讲了23种设计模式中的前几个,下面我们继续,先看前几篇文章。
1. 23种设计模式(一)
2. 23种设计模式(二)
3. 23种设计模式(三)
4. 23种设计模式(四)
5. 23种设计模式(五)
6. 23种设计模式(六)
7. 23种设计模式(七)
8. 23种设计模式(八)
详述
十七、桥接模式——Bridge
将抽象部分与它的实现部分分离,使它们都可以独立地变化。先看一张原理图。
桥接模式原理图在看一下写的代码结构图。
代码结构原理图下面看代码。
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> 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 <Foundation/Foundation.h>
#import "HCDSoftware.h"
@interface HCDPhone : NSObject
@property (nonatomic, strong) id<HCDSoftware> 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 <Foundation/Foundation.h>
@protocol HCDSoftware <NSObject>
- (void)run;
@end
9. HCDGameSoftware.h
#import <Foundation/Foundation.h>
#import "HCDSoftware.h"
@interface HCDGameSoftware : NSObject <HCDSoftware>
@end
10. HCDGameSoftware.m
#import "HCDGameSoftware.h"
@implementation HCDGameSoftware
- (void)run
{
NSLog(@"运行游戏手机软件");
}
@end
11. HCDAddressListSofeware.h
#import <Foundation/Foundation.h>
#import "HCDSoftware.h"
@interface HCDAddressListSofeware : NSObject <HCDSoftware>
@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
将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化,对请求排队或记录请求日志,以及支持可撤销的操作。
先看一下代码结构图。
下面看代码。
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 <Foundation/Foundation.h>
#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 <Foundation/Foundation.h>
@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 <Foundation/Foundation.h>
#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 <Foundation/Foundation.h>
@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] 厨师考鸡肉:二十串鸡肉
结论:注意思想。
后记
风景欣赏今晚就写这么多了,祝大家也祝我自己五一快乐!未完,待续!谢谢,各位晚安!!