开源中国博客地址
demo传送门
摘要: 工厂模式 也称为 虚拟构造 ,在基类中定义创建对象接口,让子类决定实例化哪个类;工厂方法让一个类的实例化延迟到子类中进行;他解决的问题是对象的创建时机,他提供了一种的扩展的策略。
特征:1. 存在继承关系;2. 运用了多态的特征。
使用条件:1. 一个无法预期知道他要生成哪个类的对象,就让其子类决定;2. 创建大量相同类的对象
创建Person类
创建Person类继承NSObject;作为Man,Woman的基类
#import <Foundation/Foundation.h>
@interface Person : NSObject
/**
记录子类名字
*/
@property (nonatomic,strong)NSString *name;
/**
初始化基类,根据条件初始化出相应的子类
@param condition 条件(这里 1 为Man, 2 为 Woman)
@return 获取具体的子类
*/
- (instancetype)initPersonSubclassWithCondition:(NSInteger)condition;
/**
子类实现对应的方法
@return 要执行的操作
*/
- (NSString *)likeSomeOne;
@end
Person根据条件初始化具体子类
#import "Person.h"
#import "Man.h"
#import "Woman.h"
@implementation Person
- (instancetype)initPersonSubclassWithCondition:(NSInteger)condition
{
if (condition == 1) {
Man *man = [[Man alloc]init];
man.name = @"我是男人";
return man;
}
if (condition == 2) {
Woman *woman = [[Woman alloc]init];
woman.name = @"我是女人";
return woman;
}
return nil;
}
- (NSString *)likeSomeOne
{
return nil;
}
@end
创建Person子类 Man,Woman
子类仅仅重写父类方法
#import "Man.h"
@implementation Man
- (NSString *)likeSomeOne
{
return @"我是男人,我喜欢女人";
}
@end
#import "Woman.h"
@implementation Woman
- (NSString *)likeSomeOne
{
return @"我是女人,我喜欢男人";
}
@end
创建Animal类
创建Animal类作为Dog,Cat的基类
#import <Foundation/Foundation.h>
@interface Animal : NSObject
/**
记录名字
*/
@property (nonatomic,strong)NSString *name;
/**
初始化基类,根据条件初始化出相应的子类
@param condition 条件(这里 1 为Dog, 2 为 Cat)
@return 获取具体的子类
*/
- (instancetype)initAnimalSubclassWithCondition:(NSInteger)condition;
/**
子类实现对应的方法
@return 要执行的操作
*/
- (NSString *)likeEatFood;
@end
Animal根据条件初始化具体子类
#import "Animal.h"
#import "Dog.h"
#import "Cat.h"
@implementation Animal
- (instancetype)initAnimalSubclassWithCondition:(NSInteger)condition
{
if (condition == 1) {
Dog *dog = [[Dog alloc]init];
dog.name = @"我是🐶";
return dog;
}
if (condition == 2) {
Cat *cat = [[Cat alloc]init];
cat.name = @"我是🐱";
return cat;
}
return nil;
}
- (NSString *)likeEatFood
{
return nil;
}
@end
创建Animal子类 Dog,Cat
子类仅仅重写父类方法
#import "Dog.h"
@implementation Dog
- (NSString *)likeEatFood
{
return @"我是🐶,我喜欢吃💩";
}
@end
#import "Cat.h"
@implementation Cat
- (NSString *)likeEatFood
{
return @"我是🐱,我喜欢吃🐟";
}
@end
创建工厂类
在基类中定义创建对象接口,根据条件让子类决定实例化哪个类
#import <Foundation/Foundation.h>
@class Person;
@class Animal;
@interface Factory : NSObject
/**
工厂Person子类
*/
@property (nonatomic,strong)Person *person;
/**
工厂Animal子类
*/
@property (nonatomic,strong)Animal *animal;
/**
初始化工厂,根据condition 条件初始化 Person/Animal基类,根据subclassCondt初始化基类的子类
@param condition 初始化基类条件(1为 Person, 2 为Animal)
@param subclassCondt 初始化基类子类 (Person类:(1 为Man, 2,为Woman); Animal类:(1 为 Dog, 2 为 Cat))
@return 获取具体的子类
*/
- (instancetype)initFactorySubclassWithCondition:(NSInteger)condition AndBaseSubclassCondition:(NSInteger)subclassCondt;
/**
获取子类名称
*/
- (void)getFactorySubClass;
@end
#import "Factory.h"
#import "Person.h"
#import "Animal.h"
@implementation Factory
- (instancetype)initFactorySubclassWithCondition:(NSInteger)condition AndBaseSubclassCondition:(NSInteger)subclassCondt
{
if (condition == 1) {
Person *person = [[Person alloc]initPersonSubclassWithCondition:subclassCondt];
self.person = person;
}
if (condition == 2) {
Animal *animal = [[Animal alloc]initAnimalSubclassWithCondition:subclassCondt];
self.animal = animal;
}
return self;
}
- (void)getFactorySubClass
{
if (self.person != nil) {
NSLog(@"%@----%@",self.person.name,[self.person likeSomeOne]);
}
if (self.animal != nil) {
NSLog(@"%@----%@",self.animal.name,[self.animal likeEatFood]);
}
}
@end
控制器中调用
#import "ViewController.h"
#import "Factory.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
Factory *man = [[Factory alloc]initFactorySubclassWithCondition:1 AndBaseSubclassCondition:1];
[man getFactorySubClass];
Factory *woman = [[Factory alloc]initFactorySubclassWithCondition:1 AndBaseSubclassCondition:2];
[woman getFactorySubClass];
Factory *dog = [[Factory alloc]initFactorySubclassWithCondition:2 AndBaseSubclassCondition:1];
[dog getFactorySubClass];
Factory *cat = [[Factory alloc]initFactorySubclassWithCondition:2 AndBaseSubclassCondition:2];
[cat getFactorySubClass];
}
屏幕快照 2017-09-05 上午12.32.32
网友评论