object c 工厂设计模式

//
//  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







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值