//
// main.m
// FactoryStyleDemo
//
// Created by 千雅爸爸 on 16/10/7.
// Copyright © 2016年 kodulf. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Student.h"
#import "Person.h"
#import "Police.h"
#import "PersonFactory.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
//当涉及的类,不能够完全预测类型的时候,例如父类
//工厂提供了一种可以将一个类的实例话延迟到子类中执行
//首先创建一个person 类,然后创建一个student 类,然后是集成person 的police的类
//没有工厂模式的情况:
Student *student =[Student person];
//block 相当于是java的匿名内部类
[student setWorkBlock:^(Person *person){
Student *temp = (Student *)person;//强转成student类型
[temp readBookWithBookName:@"ios"];
}];
[student doWork];
Police *police = [Police person];
[police setWorkBlock:^(Person *person){
Police *temp = (Police *)person;
[temp catchThiefWithName:@"Tom"];
}];
[police doWork];
//可以看到police 和student的逻辑很相似,
//这个时候就想到了工厂设计模式,解决了代码的书写,业务逻辑清晰,就是隐藏子类模式的初始化设置。
//现在去创建一个PersonFactory类
NSLog(@"++++++++开始工厂的使用++++++++++");
//++++++++开始工厂的使用++++++++++
//解决了代码的书写,业务逻辑清晰,隐藏子类模式的初始化设置
//其实就是封装后,方便子类的创建和使用,简化初始化。
Person *person =[PersonFactory personWithTask:@"ReadBook"];
[person doWork];
Person *catch =[PersonFactory personWithTask:@"CatchThief"];
[catch doWork];
}
return 0;
}
//
// PersonFactory.h
// FactoryStyleDemo
//
// Created by 千雅爸爸 on 16/10/7.
// Copyright © 2016年 kodulf. All rights reserved.
//
#import "Person.h"
@interface PersonFactory : Person
+(Person *) personWithTask:(NSString *)task;
@end
//
// PersonFactory.m
// FactoryStyleDemo
//
// Created by 千雅爸爸 on 16/10/7.
// Copyright © 2016年 kodulf. All rights reserved.
//
#import "PersonFactory.h"
#import "Student.h"
#import "Police.h"
#import "Person.h"
@implementation PersonFactory
//初始化都在这里执行了,封装在了这里,外面就不用去写了
+(Person *) personWithTask:(NSString *)task{
Person *person = nil;
if([task isEqualToString:@"ReadBook"]){
person = [Student person];
person.name = @"xiaoming";
person.age = 16;
[person setWorkBlock:^(Person *person){
Student *temp = (Student *)person;
[temp readBookWithBookName:@"ios"];
}];
}else if([task isEqualToString:@"CatchThief"]){
person = [Police person];
person.name = @"police";
person.age = 29;
Police *police = (Police *)person;
[police catchThiefWithName:@"jack"];
}else{
person = [Police person];
person.name = @"police";
person.age = 29;
}
return person;
}
@end
//
// Person.h
// FactoryStyleDemo
//
// Created by 千雅爸爸 on 16/10/7.
// Copyright © 2016年 kodulf. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Person : NSObject
//要想子类能够使用,那么这里需要写上
{
@protected
NSString *_name;
NSInteger _age;
}
@property (nonatomic,copy) NSString *name;
@property (nonatomic,assign) NSInteger age;
//通过block 类型来添加不同的具体的工作, 这里的block 就相当于java中匿名内部类,setonclickListener(new onclicklistener(){})。。
@property (nonatomic,copy)void (^workBlock)(Person *person);
+(instancetype)person;
-(void)doWork;
@end
//
// Person.m
// FactoryStyleDemo
//
// Created by 千雅爸爸 on 16/10/7.
// Copyright © 2016年 kodulf. All rights reserved.
//
#import "Person.h"
@implementation Person
//注意这里是self alloc init
+(instancetype) person{
return [[self alloc]init];
}
-(void)doWork{
if(self.workBlock){
self.workBlock(self);
}
}
@end
//
// Student.h
// FactoryStyleDemo
//
// Created by 千雅爸爸 on 16/10/7.
// Copyright © 2016年 kodulf. All rights reserved.
//
#import "Person.h"
@interface Student : Person
-(void)readBookWithBookName:(NSString *)bookname;
@end
//
// Student.m
// FactoryStyleDemo
//
// Created by 千雅爸爸 on 16/10/7.
// Copyright © 2016年 kodulf. All rights reserved.
//
#import "Student.h"
@implementation Student
-(void)readBookWithBookName:(NSString *)bookname{
NSLog(@"name =%@,bookname=%@",_name,bookname);
}
@end
//
// Police.h
// FactoryStyleDemo
//
// Created by 千雅爸爸 on 16/10/7.
// Copyright © 2016年 kodulf. All rights reserved.
//
#import "Person.h"
@interface Police : Person
-(void)catchThiefWithName:(NSString *)thiefName;
@end
//
// Police.m
// FactoryStyleDemo
//
// Created by 千雅爸爸 on 16/10/7.
// Copyright © 2016年 kodulf. All rights reserved.
//
#import "Police.h"
@implementation Police
-(void)catchThiefWithName:(NSString *)thiefName{
NSLog(@"警察%@抓小偷%@",_name,thiefName);
}
@end