23种设计模式(六)

版本记录

版本号 时间
V1.0 2017.04.27

前言

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

详述

十、抽象工厂模式——Abstract Factory

  提供了一个创建一些列相关或相互依赖对象的接口,而无需指定它们具体的类。

看代码结构。

代码结构

看代码。

1. ViewController.m
#import "ViewController.h"
#import "HCDFactory.h"
#import "HCDSqlserverFactory.h"
#import "HCDAccessFactory.h"
#import "HCDDepartment.h"
#import "HCDUser.h"

#import "SQLDepartment.h"
#import "SQLUser.h"

typedef id<HCDFactory> HCDFactory;
typedef id<HCDDepartment> HCDDepartment;
typedef id<HCDUser> HCDUser;

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    HCDFactory factory = [[HCDSqlserverFactory alloc]init];
    HCDDepartment department = [factory createDepartment];
    [department insertDepartment:[[SQLDepartment alloc]init]];
    [department getDepartment];
    
    HCDFactory factory1 = [[HCDAccessFactory alloc]init];
    HCDDepartment department1 = [factory1 createDepartment];
    [department1 insertDepartment:[[SQLDepartment alloc]init]];
    [department1 getDepartment];
}

@end
2. SQLUser.h

#import <Foundation/Foundation.h>

@interface SQLUser : NSObject

@end

3. SQLUser.m
#import "SQLUser.h"

@implementation SQLUser

@end

4. SQLDepartment.h

#import <Foundation/Foundation.h>

@interface SQLDepartment : NSObject

@end

5. SQLDepartment.m
#import "SQLDepartment.h"

@implementation SQLDepartment

@end

6. HCDFactory.h

#import <Foundation/Foundation.h>
#import "HCDUser.h"
#import "HCDDepartment.h"

@protocol HCDFactory <NSObject>

- (id<HCDUser>)createUser;

- (id<HCDDepartment>)createDepartment;

@end


7. HCDSqlserverFactory.h
#import <Foundation/Foundation.h>
#import "HCDFactory.h"

@interface HCDSqlserverFactory : NSObject <HCDFactory>

@end
8. HCDSqlserverFactory.m
#import "HCDSqlserverFactory.h"
#import "HCDSqlserverUser.h"
#import "HCDSqlserverDepartment.h"

@implementation HCDSqlserverFactory

- (id<HCDUser>)createUser
{
    return [[HCDSqlserverUser alloc]init];
}

- (id<HCDDepartment>)createDepartment
{
    return [[HCDSqlserverDepartment alloc]init];
}

@end


9. HCDAccessFactory.h

#import <Foundation/Foundation.h>
#import "HCDFactory.h"

@interface HCDAccessFactory : NSObject <HCDFactory>

@end



10. HCDAccessFactory.m
#import "HCDAccessFactory.h"
#import "HCDAccessDepartment.h"
#import "HCDAccessUser.h"

@implementation HCDAccessFactory

- (id<HCDUser>)createUser
{
    return [[HCDAccessUser alloc]init];
}

- (id<HCDDepartment>)createDepartment
{
    return [[HCDAccessDepartment alloc]init];
}

@end

11. HCDDepartment.h

#import <Foundation/Foundation.h>
#import "SQLDepartment.h"

@protocol HCDDepartment <NSObject>

- (void)insertDepartment:(SQLDepartment *)department;

- (SQLDepartment *)getDepartment;

@end


12. HCDSqlserverDepartment.h

#import <Foundation/Foundation.h>
#import "HCDDepartment.h"

@interface HCDSqlserverDepartment : NSObject <HCDDepartment>

@end


13. HCDSqlserverDepartment.m
#import "HCDSqlserverDepartment.h"

@implementation HCDSqlserverDepartment

- (SQLDepartment *)getDepartment
{
    NSLog(@"新建一个Sqlserver的SQLDepartment对象");
    return [[SQLDepartment alloc]init];
}

- (void)insertDepartment:(SQLDepartment *)department
{
    NSLog(@"插入一个Sqlserver的SQLDepartment对象");
}

@end



14. HCDAccessDepartment.h
#import <Foundation/Foundation.h>
#import "HCDDepartment.h"

@interface HCDAccessDepartment : NSObject <HCDDepartment>

@end



15. HCDAccessDepartment.m
#import "HCDAccessDepartment.h"

@implementation HCDAccessDepartment

- (SQLDepartment *)getDepartment
{
    NSLog(@"新建一个Access的SQLDepartment对象");
    return [[SQLDepartment alloc]init];
}

- (void)insertDepartment:(SQLDepartment *)department
{
    NSLog(@"插入一个Access的SQLDepartment对象");
}

@end


16. HCDUser.h

#import <Foundation/Foundation.h>
#import "SQLUser.h"

@protocol HCDUser <NSObject>

- (void)insertUser:(SQLUser *)user;

- (SQLUser *)getUser;

@end



17. HCDSqlserverUser.h
#import <Foundation/Foundation.h>
#import "HCDUser.h"

@interface HCDSqlserverUser : NSObject<HCDUser>

@end


18. HCDSqlserverUser.m

#import "HCDSqlserverUser.h"

@implementation HCDSqlserverUser

- (SQLUser *)getUser
{
    NSLog(@"新建一个Sqlserver的SQLUser对象");
    return [[SQLUser alloc]init];
}

- (void)insertUser:(SQLUser *)user
{
     NSLog(@"插入一个Sqlserver的SQLUser对象");
}

@end


19. HCDAccessUser.h

#import <Foundation/Foundation.h>
#import "HCDUser.h"
#import "SQLUser.h"

@interface HCDAccessUser : NSObject <HCDUser>

@end


20. HCDAccessUser.m
#import "HCDAccessUser.h"

@implementation HCDAccessUser

- (SQLUser *)getUser
{
    NSLog(@"新建一个Access的SQLUser对象");
    return [[SQLUser alloc]init];
}

- (void)insertUser:(SQLUser *)user
{
    NSLog(@"插入一个Access的SQLUser对象");
}

@end

看结果。

2017-04-27 00:24:51.705 10抽象工厂模式[1653:47095] 插入一个Sqlserver的SQLDepartment对象
2017-04-27 00:24:51.705 10抽象工厂模式[1653:47095] 新建一个Sqlserver的SQLDepartment对象
2017-04-27 00:24:51.705 10抽象工厂模式[1653:47095] 插入一个Access的SQLDepartment对象
2017-04-27 00:24:51.713 10抽象工厂模式[1653:47095] 新建一个Access的SQLDepartment对象


结论:看思想吧。


十一、状态模式——State

  当一个对象的内在状态改变时允许改变其行为,这个对象看起来像是改变了其类。

看状态模式原理图。

状态模式结构图

看代码结构图。

代码结构图

看代码。

1. ViewController.m

#import "ViewController.h"
#import "HCDWork.h"
#import "HCDNoonState.h"
#import "HCDAfternoonState.h"
#import "HCDEventState.h"
#import "HCDSleepState.h"
#import "HCDRestState.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    HCDWork *work = [[HCDWork alloc]init];
    [work writeProgram];
    work.state = [[HCDAfternoonState alloc]init];
    [work writeProgram];
    work.state = [[HCDEventState alloc]init];
    [work writeProgram];
    work.state = [[HCDSleepState alloc]init];
    [work writeProgram];
}

@end


2. HCDState.h

#import <Foundation/Foundation.h>

@class HCDWork;

@protocol HCDState <NSObject>

- (void)writeProgram:(HCDWork *)work;

@end


3. HCDWork.h

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "HCDState.h"

@interface HCDWork : NSObject

@property (nonatomic, strong) id<HCDState> state;
@property (nonatomic, assign) CGFloat hour;
@property (nonatomic, assign) BOOL finished;

- (void)writeProgram;

@end



4. HCDWork.m
#import "HCDWork.h"
#import "HCDForenoonState.h"

@implementation HCDWork

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.state = [[HCDForenoonState alloc]init];
    }
    return self;
}

- (void)writeProgram{
    [self.state writeProgram:self];
}

@end

5. HCDForenoonState.h

#import <Foundation/Foundation.h>
#import "HCDState.h"

@interface HCDForenoonState : NSObject<HCDState>

@end

6. HCDForenoonState.m
#import "HCDForenoonState.h"
#import "HCDWork.h"
#import "HCDNoonState.h"

@implementation HCDForenoonState

- (void)writeProgram:(HCDWork *)work
{
    if (work.hour < 12) {
         NSLog(@"当前时间:{%.f}点,上午工作,精神百倍", work.hour);
    }else{
        work.state = [[HCDNoonState alloc] init];
        [work writeProgram];
    }
}

@end


7. HCDNoonState.h
#import <Foundation/Foundation.h>
#import "HCDState.h"

@interface HCDNoonState : NSObject<HCDState>

@end

8. HCDNoonState.m
#import "HCDNoonState.h"
#import "HCDAfternoonState.h"
#import "HCDWork.h"

@implementation HCDNoonState

- (void)writeProgram:(HCDWork *)work
{
    if (work.hour < 13) {
        NSLog(@"当前时间:{%.f}点,饿了,午饭;犯困,午休", work.hour);
    } else {
        work.state = [[HCDAfternoonState alloc] init];
        [work writeProgram];
    }
}

@end

9. HCDAfternoonState.h

#import <Foundation/Foundation.h>
#import "HCDState.h"

@interface HCDAfternoonState : NSObject <HCDState>

@end


10. HCDAfternoonState.m

#import "HCDAfternoonState.h"
#import "HCDEventState.h"
#import "HCDWork.h"

@implementation HCDAfternoonState

- (void)writeProgram:(HCDWork *)work
{
    if (work.hour < 17) {
        NSLog(@"当前时间:{%.f}点,下午状态还不错,继续努力", work.hour);
    } else {
        work.state = [[HCDEventState alloc] init];
        [work writeProgram];
    }
}

@end



11. HCDEventState.h
#import <Foundation/Foundation.h>
#import "HCDState.h"
#import "HCDRestState.h"
#import "HCDSleepState.h"

@interface HCDEventState : NSObject <HCDState>

@end


12. HCDEventState.m

#import "HCDEventState.h"
#import "HCDWork.h"
#import "HCDRestState.h"
#import "HCDSleepState.h"

@implementation HCDEventState

- (void)writeProgram:(HCDWork *)work
{
    if (work.finished) {
        work.state = [[HCDRestState alloc] init];
        [work writeProgram];
    } else {
        if (work.hour < 21) {
            NSLog(@"当前时间:{%.f}点,加班哦,疲累之极", work.hour);
        } else {
            work.state = [[HCDSleepState alloc] init];
            [work writeProgram];
        }
    }
}

@end



13. HCDSleepState.h
#import <Foundation/Foundation.h>
#import "HCDState.h"

@interface HCDSleepState : NSObject <HCDState>

@end


14. HCDSleepState.m

#import "HCDSleepState.h"
#import "HCDWork.h"

@implementation HCDSleepState

- (void)writeProgram:(HCDWork *)work
{
    NSLog(@"当前时间:{%.f}点,不行了,睡着了", work.hour);
}

@end



15. HCDRestState.h
#import <Foundation/Foundation.h>
#import "HCDState.h"

@interface HCDRestState : NSObject <HCDState>

@end



16. HCDRestState.m
#import "HCDRestState.h"
#import "HCDWork.h"

@implementation HCDRestState

- (void)writeProgram:(HCDWork *)work
{
    NSLog(@"当前时间:{%.f}点,下班回家了", work.hour);
}

@end


看结果。

2017-04-27 01:03:52.690 11状态模式[2123:73003] 当前时间:{0}点,上午工作,精神百倍
2017-04-27 01:03:52.692 11状态模式[2123:73003] 当前时间:{0}点,下午状态还不错,继续努力
2017-04-27 01:03:52.692 11状态模式[2123:73003] 当前时间:{0}点,加班哦,疲累之极
2017-04-27 01:03:52.693 11状态模式[2123:73003] 当前时间:{0}点,不行了,睡着了

结论:注意思想。

后记

  未完,待续,我会有时间继续把剩下的那些模式写完。谢谢大家。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 205,236评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,867评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,715评论 0 340
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,899评论 1 278
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,895评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,733评论 1 283
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,085评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,722评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,025评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,696评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,816评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,447评论 4 322
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,057评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,009评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,254评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,204评论 2 352
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,561评论 2 343

推荐阅读更多精彩内容