版本記錄
版本號 | 時間 |
---|---|
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)模式原理圖援雇。
看代碼結構圖。
看代碼椎扬。
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ù)把剩下的那些模式寫完。謝謝大家票唆。