版本记录
版本号 | 时间 |
---|---|
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}点,不行了,睡着了
结论:注意思想。
后记
未完,待续,我会有时间继续把剩下的那些模式写完。谢谢大家。