23種設計模式(六)

版本記錄

版本號 時間
V1.0 2017.04.27

前言

前面講了23種設計模式中的前幾個片仿,下面我們繼續(xù),先看前幾篇文章煤篙。
1. 23種設計模式(一)
2. 23種設計模式(二)
3. 23種設計模式(三)
4. 23種設計模式(四)
5. 23種設計模式(五)

詳述

十嘴瓤、抽象工廠模式——Abstract Factory

??提供了一個創(chuàng)建一些列相關或相互依賴對象的接口,而無需指定它們具體的類轴猎。

看代碼結構嵌莉。

代碼結構

看代碼。

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對象


結論:看思想吧锐峭。


十一中鼠、狀態(tài)模式——State

??當一個對象的內在狀態(tài)改變時允許改變其行為,這個對象看起來像是改變了其類沿癞。

看狀態(tài)模式原理圖援雇。

狀態(tài)模式結構圖

看代碼結構圖。

代碼結構圖

看代碼椎扬。

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}點川陆,下午狀態(tài)還不錯,繼續(xù)努力", 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狀態(tài)模式[2123:73003] 當前時間:{0}點萄焦,上午工作究恤,精神百倍
2017-04-27 01:03:52.692 11狀態(tài)模式[2123:73003] 當前時間:{0}點衣陶,下午狀態(tài)還不錯钉寝,繼續(xù)努力
2017-04-27 01:03:52.692 11狀態(tài)模式[2123:73003] 當前時間:{0}點蔬芥,加班哦,疲累之極
2017-04-27 01:03:52.693 11狀態(tài)模式[2123:73003] 當前時間:{0}點冒签,不行了在抛,睡著了

結論:注意思想。

后記

??未完萧恕,待續(xù)刚梭,我會有時間繼續(xù)把剩下的那些模式寫完。謝謝大家票唆。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末朴读,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子走趋,更是在濱河造成了極大的恐慌衅金,老刑警劉巖,帶你破解...
    沈念sama閱讀 207,113評論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異氮唯,居然都是意外死亡酥宴,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,644評論 2 381
  • 文/潘曉璐 我一進店門您觉,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人授滓,你說我怎么就攤上這事琳水。” “怎么了般堆?”我有些...
    開封第一講書人閱讀 153,340評論 0 344
  • 文/不壞的土叔 我叫張陵在孝,是天一觀的道長。 經常有香客問我淮摔,道長私沮,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 55,449評論 1 279
  • 正文 為了忘掉前任和橙,我火速辦了婚禮仔燕,結果婚禮上,老公的妹妹穿的比我還像新娘魔招。我一直安慰自己晰搀,他們只是感情好,可當我...
    茶點故事閱讀 64,445評論 5 374
  • 文/花漫 我一把揭開白布办斑。 她就那樣靜靜地躺著外恕,像睡著了一般。 火紅的嫁衣襯著肌膚如雪乡翅。 梳的紋絲不亂的頭發(fā)上鳞疲,一...
    開封第一講書人閱讀 49,166評論 1 284
  • 那天,我揣著相機與錄音蠕蚜,去河邊找鬼尚洽。 笑死,一個胖子當著我的面吹牛波势,可吹牛的內容都是我干的翎朱。 我是一名探鬼主播,決...
    沈念sama閱讀 38,442評論 3 401
  • 文/蒼蘭香墨 我猛地睜開眼尺铣,長吁一口氣:“原來是場噩夢啊……” “哼拴曲!你這毒婦竟也來了?” 一聲冷哼從身側響起凛忿,我...
    開封第一講書人閱讀 37,105評論 0 261
  • 序言:老撾萬榮一對情侶失蹤澈灼,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發(fā)現(xiàn)了一具尸體叁熔,經...
    沈念sama閱讀 43,601評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡委乌,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 36,066評論 2 325
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了荣回。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片遭贸。...
    茶點故事閱讀 38,161評論 1 334
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖心软,靈堂內的尸體忽然破棺而出壕吹,到底是詐尸還是另有隱情,我是刑警寧澤删铃,帶...
    沈念sama閱讀 33,792評論 4 323
  • 正文 年R本政府宣布耳贬,位于F島的核電站,受9級特大地震影響猎唁,放射性物質發(fā)生泄漏咒劲。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 39,351評論 3 307
  • 文/蒙蒙 一诫隅、第九天 我趴在偏房一處隱蔽的房頂上張望腐魂。 院中可真熱鬧,春花似錦阎肝、人聲如沸挤渔。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,352評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽判导。三九已至,卻和暖如春沛硅,著一層夾襖步出監(jiān)牢的瞬間眼刃,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,584評論 1 261
  • 我被黑心中介騙來泰國打工摇肌, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留擂红,地道東北人。 一個月前我還...
    沈念sama閱讀 45,618評論 2 355
  • 正文 我出身青樓围小,卻偏偏與公主長得像昵骤,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子肯适,可洞房花燭夜當晚...
    茶點故事閱讀 42,916評論 2 344

推薦閱讀更多精彩內容