23种设计模式(七)

版本记录

版本号 时间
V1.0 2017.04.27

前言

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

详述

十二、适配器模式——Adapter

  将一个类的接口转换成客户希望的另外一个接口,adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。

先看一下代码组织结构。

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

下面看代码。

1. ViewController.m

#import "ViewController.h"
#import "HCDPlayer.h"
#import "HCDForwards.h"
#import "HCDForeignCenter.h"
#import "HCDTranslator.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    HCDPlayer *forward = [[HCDForwards alloc]initWithName:@"maidi"];
    [forward attack];
    [forward defense];
    HCDPlayer *foreignCenter = [[HCDTranslator alloc]initWithName:@"姚明"];
    [foreignCenter attack];
    [foreignCenter defense];
    
}

@end


2. HCDPlayer.h
#import 

@interface HCDPlayer : NSObject

@property (nonatomic, copy) NSString *name;

- (void)attack;

- (void)defense;

- (instancetype)initWithName:(NSString *)name;

@end
3. HCDPlayer.m

#import "HCDPlayer.h"

@implementation HCDPlayer

- (instancetype)initWithName:(NSString *)name
{
    self = [super init];
    if (self) {
        _name = name;
    }
    return self;
}

@end


4. HCDForwards.h
#import "HCDPlayer.h"

@interface HCDForwards : HCDPlayer

@end


5. HCDForwards.m
#import "HCDForwards.h"

@implementation HCDForwards

- (void)attack
{
    NSLog(@"前锋%@进攻",self.name);
}

- (void)defense{
    NSLog(@"前锋%@防守",self.name);
}

@end

6. HCDTranslator.h

#import "HCDPlayer.h"

@class HCDForeignCenter;

@interface HCDTranslator : HCDPlayer

@property (nonatomic, strong) HCDForeignCenter *foreigncenter;

@end

7. HCDTranslator.m

#import "HCDTranslator.h"
#import "HCDForeignCenter.h"

@interface HCDTranslator ()

@end

@implementation HCDTranslator

- (instancetype)initWithName:(NSString *)name
{
    self = [super init];
    if (self) {
        _foreigncenter = [[HCDForeignCenter alloc]initWithName:name];
    }
    return self;
}

- (void)defense
{
    [self.foreigncenter foreignDefent];
}

- (void)attack
{
    [self.foreigncenter foreignAttact];
}

@end

8. HCDForeignCenter.h

#import 

@interface HCDForeignCenter : NSObject

@property (nonatomic, copy) NSString *name;

- (void)foreignAttact;

- (void)foreignDefent;

- (instancetype)initWithName:(NSString *)name;

@end

9. HCDForeignCenter.m

#import "HCDForeignCenter.h"

@implementation HCDForeignCenter

- (instancetype)initWithName:(NSString *)name
{
    self = [super init];
    if (self) {
        _name = name;
    }
    return self;
}

- (void)foreignAttact
{
    NSLog(@"外籍中锋%@进攻",self.name);
}

- (void)foreignDefent
{
    NSLog(@"外籍中锋%@防守",self.name);
}

@end


看结果。

2017-04-27 23:18:14.108 12适配器模式[845:12248] 前锋maidi进攻
2017-04-27 23:18:14.114 12适配器模式[845:12248] 前锋maidi防守
2017-04-27 23:18:14.115 12适配器模式[845:12248] 外籍中锋姚明进攻
2017-04-27 23:18:14.116 12适配器模式[845:12248] 外籍中锋姚明防守

结论:思想重要,自己体会吧。


十三、备忘录模式——Memento

  在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态,这样以后就可将该对象恢复到原先保存的状态。

先看一下原理图。

23种设计模式(七)_第2张图片
备忘录模式

看一下代码结构图。

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

下面看代码。


1. ViewController.m
#import "ViewController.h"
#import "HCDGameRoll.h"
#import "HCDGameState.h"
#import "HCDStateManager.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    HCDGameRoll *roll = [[HCDGameRoll alloc]init];
    
    [roll getState];
    HCDGameState *state = [roll saveState];
    HCDStateManager *manager = [[HCDStateManager alloc]initWithGameState:state];
    [roll fightBoss];
    [roll getState];
    [roll rebackState:manager.gameState];
    [roll getState];
    
}

@end


2. HCDGameRoll.h

#import 

@class HCDGameState;

@interface HCDGameRoll : NSObject

@property (nonatomic, assign) NSInteger life;
@property (nonatomic, assign) NSInteger attack;
@property (nonatomic, assign) NSInteger defeat;

- (void)getState;

- (HCDGameState *)saveState;

- (void)fightBoss;

- (void)rebackState:(HCDGameState *)state;

@end

3. HCDGameRoll.m

#import "HCDGameRoll.h"
#import "HCDGameState.h"

@implementation HCDGameRoll

- (instancetype)init
{
    self = [super init];
    if (self) {
        _life = 100;
        _attack = 100;
        _defeat = 100;
    }
    return self;
}

- (void)getState
{
    NSLog(@"生命力%ld,防御力%ld,攻击力%ld",self.life,self.defeat,self.attack);
}

- (HCDGameState *)saveState
{
    HCDGameState *state = [[HCDGameState alloc]init];
    state.life = self.life;
    state.defeat = self.defeat;
    state.attack = self.attack;
    return state;
}

- (void)rebackState:(HCDGameState *)state
{
    self.life = state.life;
    self.defeat = state.defeat;
    self.attack = state.attack;
}

- (void)fightBoss
{
    self.life = 0;
    self.defeat = 0;
    self.attack = 0;
}

@end


4. HCDGameState.h
#import 

@interface HCDGameState : NSObject

@property (nonatomic, assign) NSInteger life;
@property (nonatomic, assign) NSInteger attack;
@property (nonatomic, assign) NSInteger defeat;

@end


5. HCDGameState.m
#import "HCDGameState.h"

@implementation HCDGameState

@end


6. HCDStateManager.h

#import 

@class HCDGameState;

@interface HCDStateManager : NSObject

@property (nonatomic, strong) HCDGameState *gameState;

- (instancetype)initWithGameState:(HCDGameState *)gameState;

@end



7. HCDStateManager.m

#import "HCDStateManager.h"

@implementation HCDStateManager

- (instancetype)initWithGameState:(HCDGameState *)gameState
{
    self = [super init];
    if (self) {
        _gameState = gameState;
    }
    return self;
}

@end


看结果。

2017-04-27 23:48:10.706 13备忘录模式[1303:30484] 生命力100,防御力100,攻击力100
2017-04-27 23:48:10.707 13备忘录模式[1303:30484] 生命力0,防御力0,攻击力0
2017-04-27 23:48:10.708 13备忘录模式[1303:30484] 生命力100,防御力100,攻击力100

结论:注重思想。


十四、组合模式——Composite

  将对象组合成树形结构以表示“部分--整体”的层次结构,组合模式使得用户对单个对象和组合对象的使用具有一致性。

代码结构。

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

看代码。

1. ViewController.m
#import "ViewController.h"
#import "HCDConcreteCompany.h"
#import "HCDHRDepartment.h"
#import "HCDFinanceDepartment.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    HCDConcreteCompany *root = [[HCDConcreteCompany alloc]initWithName:@"总公司"];
    [root add:[[HCDHRDepartment alloc]initWithName:@"总公司人力资源部"]];
    [root add:[[HCDFinanceDepartment alloc]initWithName:@"总公司财务部"]];
    
    HCDConcreteCompany *comp = [[HCDConcreteCompany alloc]initWithName:@"上海华东分公司"];
    [comp add:[[HCDHRDepartment alloc]initWithName:@"上海华东分公司人力资源部"]];
    [comp add:[[HCDFinanceDepartment alloc]initWithName:@"上海华东分公司财务部"]];
    [root add:comp];
    
    HCDConcreteCompany *comp1 = [[HCDConcreteCompany alloc]initWithName:@"杭州办事处"];
    [comp1 add:[[HCDHRDepartment alloc]initWithName:@"杭州办事处人力资源部"]];
    [comp1 add:[[HCDFinanceDepartment alloc]initWithName:@"杭州办事处财务部"]];
    [root add:comp1];
    NSLog(@"结构图:----------------------------");
    [root display];
    NSLog(@"职责:---------------------------");
    [root lineofDuty];
}

@end

2. HCDCompany.h

#import 

@protocol HCDCompany 

- (void)add:(id)company;

- (void)remove:(id)company;

- (void)display;

- (void)lineofDuty;

@end

3. HCDConcreteCompany.h

#import 
#import "HCDCompany.h"

@interface HCDConcreteCompany : NSObject

@property (nonatomic, copy) NSString *name;

- (instancetype)initWithName:(NSString *)name;

@end



4. HCDConcreteCompany.m
#import "HCDConcreteCompany.h"

@interface HCDConcreteCompany ()

@property (nonatomic, strong) NSMutableArray *childList;

@end

@implementation HCDConcreteCompany

- (instancetype)initWithName:(NSString *)name
{
    self = [super init];
    if (self) {
        _name = name;
        _childList = [NSMutableArray array];
    }
    return self;
}

- (void)add:(id)company
{
    [self.childList addObject:company];
}

- (void)remove:(id)company
{
    [self.childList removeObject:company];
}

- (void)display
{
    NSLog(@"%@的子公司",self.name);
    for (id company in self.childList) {
        [company display];
    }
}

- (void)lineofDuty
{
    NSLog(@"%@的子公司的职责",self.name);
    for (id company in self.childList) {
        [company lineofDuty];
    }
}

@end


5. HCDHRDepartment.h

#import 
#import "HCDCompany.h"

@interface HCDHRDepartment : NSObject

@property (nonatomic, copy) NSString *name;

- (instancetype)initWithName:(NSString *)name;

@end


6. HCDHRDepartment.m

#import "HCDHRDepartment.h"

@implementation HCDHRDepartment

- (instancetype)initWithName:(NSString *)name
{
    self = [super init];
    if (self) {
        _name = name;
    }
    return self;
}

- (void)add:(id)company
{
    
}

- (void)display
{
    NSLog(@"%@的HR部门",self.name);
}

- (void)remove:(id)company
{

}

- (void)lineofDuty
{
    NSLog(@"%@,培训员工",self.name);
}

@end


7. HCDFinanceDepartment.h

#import 
#import "HCDCompany.h"

@interface HCDFinanceDepartment : NSObject

@property (nonatomic, copy) NSString *name;

- (instancetype)initWithName:(NSString *)name;

@end

8. HCDFinanceDepartment.m

#import "HCDFinanceDepartment.h"

@implementation HCDFinanceDepartment

- (instancetype)initWithName:(NSString *)name
{
    self = [super init];
    if (self) {
        _name = name;
    }
    return self;
}

- (void)add:(id)company
{
    
}

- (void)display
{
    NSLog(@"%@,的财务部门",self.name);
}

- (void)remove:(id)company
{
    
}

- (void)lineofDuty
{
    NSLog(@"%@,给员工发钱",self.name);
}

@end

看结果。

2017-04-27 23:59:53.524 14组合模式[1585:41391] 结构图:----------------------------
2017-04-27 23:59:53.524 14组合模式[1585:41391] 总公司的子公司
2017-04-27 23:59:53.525 14组合模式[1585:41391] 总公司人力资源部的HR部门
2017-04-27 23:59:53.525 14组合模式[1585:41391] 总公司财务部,的财务部门
2017-04-27 23:59:53.526 14组合模式[1585:41391] 上海华东分公司的子公司
2017-04-27 23:59:53.527 14组合模式[1585:41391] 上海华东分公司人力资源部的HR部门
2017-04-27 23:59:53.527 14组合模式[1585:41391] 上海华东分公司财务部,的财务部门
2017-04-27 23:59:53.528 14组合模式[1585:41391] 杭州办事处的子公司
2017-04-27 23:59:53.528 14组合模式[1585:41391] 杭州办事处人力资源部的HR部门
2017-04-27 23:59:53.529 14组合模式[1585:41391] 杭州办事处财务部,的财务部门
2017-04-27 23:59:53.529 14组合模式[1585:41391] 职责:---------------------------
2017-04-27 23:59:53.529 14组合模式[1585:41391] 总公司的子公司的职责
2017-04-27 23:59:53.530 14组合模式[1585:41391] 总公司人力资源部,培训员工
2017-04-27 23:59:53.531 14组合模式[1585:41391] 总公司财务部,给员工发钱
2017-04-27 23:59:53.532 14组合模式[1585:41391] 上海华东分公司的子公司的职责
2017-04-27 23:59:53.533 14组合模式[1585:41391] 上海华东分公司人力资源部,培训员工
2017-04-27 23:59:53.533 14组合模式[1585:41391] 上海华东分公司财务部,给员工发钱
2017-04-27 23:59:53.534 14组合模式[1585:41391] 杭州办事处的子公司的职责
2017-04-27 23:59:53.534 14组合模式[1585:41391] 杭州办事处人力资源部,培训员工
2017-04-27 23:59:53.535 14组合模式[1585:41391] 杭州办事处财务部,给员工发钱

结果:自己看思想。

后记

  未完,待续,谢谢大家对我的支持。一样美图镇楼,休息。

23种设计模式(七)_第5张图片
风景图

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