工廠模式,Cell工廠實(shí)例

未標(biāo)題-1.jpg

開(kāi)源中國(guó)博客地址
demo傳送門(mén)

摘要: 工廠模式 也稱(chēng)為 虛擬構(gòu)造 ,在基類(lèi)中定義創(chuàng)建對(duì)象接口,讓子類(lèi)決定實(shí)例化哪個(gè)類(lèi);工廠方法讓一個(gè)類(lèi)的實(shí)例化延遲到子類(lèi)中進(jìn)行;他解決的問(wèn)題是對(duì)象的創(chuàng)建時(shí)機(jī),他提供了一種的擴(kuò)展的策略。
特征:1. 存在繼承關(guān)系;2. 運(yùn)用了多態(tài)的特征。
使用條件:1. 一個(gè)無(wú)法預(yù)期知道他要生成哪個(gè)類(lèi)的對(duì)象,就讓其子類(lèi)決定;2. 創(chuàng)建大量相同類(lèi)的對(duì)象

創(chuàng)建Person類(lèi)

創(chuàng)建Person類(lèi)繼承NSObject;作為Man,Woman的基類(lèi)

#import <Foundation/Foundation.h>

@interface Person : NSObject

/**
 記錄子類(lèi)名字
 */
@property (nonatomic,strong)NSString *name;

/**
 初始化基類(lèi),根據(jù)條件初始化出相應(yīng)的子類(lèi)
 
 @param condition 條件(這里 1 為Man, 2 為 Woman)
 @return 獲取具體的子類(lèi)
 */
- (instancetype)initPersonSubclassWithCondition:(NSInteger)condition;

/**
 子類(lèi)實(shí)現(xiàn)對(duì)應(yīng)的方法
 
 @return 要執(zhí)行的操作
 */
- (NSString *)likeSomeOne;

@end

Person根據(jù)條件初始化具體子類(lèi)

#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
創(chuàng)建Person子類(lèi) Man,Woman

子類(lèi)僅僅重寫(xiě)父類(lèi)方法

#import "Man.h"

@implementation Man
- (NSString *)likeSomeOne
{
    return @"我是男人,我喜歡女人";
}
@end
#import "Woman.h"

@implementation Woman
- (NSString *)likeSomeOne
{
    return @"我是女人,我喜歡男人";
}
@end

創(chuàng)建Animal類(lèi)

創(chuàng)建Animal類(lèi)作為Dog,Cat的基類(lèi)

#import <Foundation/Foundation.h>

@interface Animal : NSObject
/**
 記錄名字
 */
@property (nonatomic,strong)NSString *name;

/**
 初始化基類(lèi),根據(jù)條件初始化出相應(yīng)的子類(lèi)
 
 @param condition 條件(這里 1 為Dog, 2 為 Cat)
 @return 獲取具體的子類(lèi)
 */
- (instancetype)initAnimalSubclassWithCondition:(NSInteger)condition;

/**
 子類(lèi)實(shí)現(xiàn)對(duì)應(yīng)的方法
 
 @return 要執(zhí)行的操作
 */
- (NSString *)likeEatFood;

@end

Animal根據(jù)條件初始化具體子類(lèi)

#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
創(chuàng)建Animal子類(lèi) Dog,Cat

子類(lèi)僅僅重寫(xiě)父類(lèi)方法

#import "Dog.h"

@implementation Dog
- (NSString *)likeEatFood
{
    return @"我是??,我喜歡吃??";
}
@end
#import "Cat.h"

@implementation Cat
- (NSString *)likeEatFood
{
    return @"我是??,我喜歡吃??";
}
@end

創(chuàng)建工廠類(lèi)

在基類(lèi)中定義創(chuàng)建對(duì)象接口,根據(jù)條件讓子類(lèi)決定實(shí)例化哪個(gè)類(lèi)

#import <Foundation/Foundation.h>
@class Person;
@class Animal;

@interface Factory : NSObject

/**
 工廠Person子類(lèi)
 */
@property (nonatomic,strong)Person *person;
/**
 工廠Animal子類(lèi)
 */
@property (nonatomic,strong)Animal *animal;

/**
 初始化工廠,根據(jù)condition 條件初始化 Person/Animal基類(lèi),根據(jù)subclassCondt初始化基類(lèi)的子類(lèi)
 
 @param condition 初始化基類(lèi)條件(1為 Person, 2 為Animal)
 @param subclassCondt 初始化基類(lèi)子類(lèi) (Person類(lèi):(1 為Man, 2,為Woman); Animal類(lèi):(1 為 Dog, 2 為 Cat))
 @return 獲取具體的子類(lèi)
 */
- (instancetype)initFactorySubclassWithCondition:(NSInteger)condition AndBaseSubclassCondition:(NSInteger)subclassCondt;

/**
 獲取子類(lèi)名稱(chēng)
 */
- (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

控制器中調(diào)用

#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 工廠實(shí)例

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

最后來(lái)一波無(wú)恥的廣告:淘劵吧

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容