美文网首页iOS程序员
工厂模式,Cell工厂实例

工厂模式,Cell工厂实例

作者: 逾期的誓言 | 来源:发表于2017-09-05 00:33 被阅读62次
未标题-1.jpg
开源中国博客地址
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

Cell 工厂实例

屏幕快照 2017-12-04 下午3.23.13.png 屏幕快照 2017-12-04 下午3.24.26.png

最后来一波无耻的广告:淘劵吧

相关文章

  • 工厂模式,Cell工厂实例

    摘要: 工厂模式 也称为 虚拟构造 ,在基类中定义创建对象接口,让子类决定实例化哪个类;工厂方法让一个类的实例化延...

  • 设计模式

    设计模式 工厂模式生成bean 工厂模式分为静态工厂和实例工厂 静态工厂: 实例工厂

  • 设计模式-3种工厂模式

    工厂模式包括:简单工厂模式,工厂方法模式,抽象工厂模式 简单工厂模式 工厂方法根据参数直接创建实例:工厂->产品 ...

  • 设计模式-工厂模式

    工厂模式概念 实例化对象,用工厂方法代替new操作。工厂模式包括工厂方法模式和抽象工厂模式。抽象工厂模式是工厂模式...

  • 抽象工厂模式

    一、抽象工厂模式介绍 二、抽象工厂模式代码实例

  • 1: 简单工厂模式

    一. 基本概念 定义 实例化对象,用工厂方法代替new操作 工厂模式包括工厂方法模式和抽象工厂模式 抽象工厂模式是...

  • 工厂方法模式

    一、工厂方法模式介绍 二、工厂方法模式代码实例

  • 设计模式之工厂方法模式

    场景1: 场景2: 工厂模式的概念 实例化对象,用工厂方法代替 new 操作。工厂模式包括工厂方法模式和抽象工厂模...

  • 简单工厂模式、工厂模式

    简单工厂模式类图 工厂方法模式类图 比较 简单工厂模式包含生成类实例的判断逻辑,工厂方法模式把判断...

  • Spring Bean的三种管理方式

    1.使用类构造器实例化(默认无参数) 2.使用静态工厂方法实例化(简单工厂模式) 3.使用实例工厂方法实例化(工厂...

网友评论

本文标题:工厂模式,Cell工厂实例

本文链接:https://www.haomeiwen.com/subject/tpbyjxtx.html