iOS设计模式 - 抽象工厂

iOS设计模式 - 抽象工厂

 

原理图

 

说明

1. 抽象工厂指的是提供一个创建一系列相关或者相互依赖对象的接口,而无需指定它们具体的类

2. 如果多个类有相同的行为,但实际实现不同,则可能需要某种抽象类型作为其父类被继承,抽象类型定义了所有相关具体类将共有的共同行为

 

 

复制代码
//
//  BrandingFactory.h
//  AbstractFactoryPattern
//
//  Created by YouXianMing on 15/8/2.
//  Copyright (c) 2015年 YouXianMing. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface BrandingFactory : NSObject

/**
 *  抽象工厂方法
 *
 *  @return 具体的工厂
 */
+ (BrandingFactory *)factory;

/**
 *  该工厂生产的brandedView(由具体工厂构造)
 *
 *  @return 生产好的brandedView
 */
- (UIView *)brandedView;

/**
 *  该工厂生产的brandedMainButton(由具体工厂构造)
 *
 *  @return 生产好的brandedMainButton
 */
- (UIButton *)brandedMainButton;

@end
复制代码
复制代码
//
//  BrandingFactory.m
//  AbstractFactoryPattern
//
//  Created by YouXianMing on 15/8/2.
//  Copyright (c) 2015年 YouXianMing. All rights reserved.
//

#import "BrandingFactory.h"
#import "AcmeBrandingFactory.h"
#import "SierraBrandingFactory.h"

@implementation BrandingFactory

+ (BrandingFactory *)factory {
    
    if ([[self class] isSubclassOfClass:[AcmeBrandingFactory class]]) {
        
        return [AcmeBrandingFactory new];
        
    } else if ([[self class] isSubclassOfClass:[SierraBrandingFactory class]]) {
    
        return [SierraBrandingFactory new];
        
    } else {
    
        return nil;
    }
}

- (UIView *)brandedView {

    return nil;
}

- (UIButton *)brandedMainButton {

    return nil;
}

@end
复制代码
复制代码
//
//  AcmeBrandingFactory.h
//  AbstractFactoryPattern
//
//  Created by YouXianMing on 15/8/2.
//  Copyright (c) 2015年 YouXianMing. All rights reserved.
//

#import "BrandingFactory.h"

@interface AcmeBrandingFactory : BrandingFactory

@end
复制代码
复制代码
//
//  AcmeBrandingFactory.m
//  AbstractFactoryPattern
//
//  Created by YouXianMing on 15/8/2.
//  Copyright (c) 2015年 YouXianMing. All rights reserved.
//

#import "AcmeBrandingFactory.h"

@implementation AcmeBrandingFactory

- (UIView *)brandedView {
    
    NSLog(@"AcmeBrandedView");
    return nil;
}

- (UIButton *)brandedMainButton {
    
    NSLog(@"AcmeBrandedMainButton");
    return nil;
}

@end
复制代码
复制代码
//
//  SierraBrandingFactory.h
//  AbstractFactoryPattern
//
//  Created by YouXianMing on 15/8/2.
//  Copyright (c) 2015年 YouXianMing. All rights reserved.
//

#import "BrandingFactory.h"

@interface SierraBrandingFactory : BrandingFactory

@end
复制代码
复制代码
//
//  SierraBrandingFactory.m
//  AbstractFactoryPattern
//
//  Created by YouXianMing on 15/8/2.
//  Copyright (c) 2015年 YouXianMing. All rights reserved.
//

#import "SierraBrandingFactory.h"

@implementation SierraBrandingFactory

- (UIView *)brandedView {
    
    NSLog(@"SierraBrandedView");
    return nil;
}

- (UIButton *)brandedMainButton {
    
    NSLog(@"SierraBrandedMainButton");
    return nil;
}

@end
复制代码

 

分析

关系原理图

 

posted on 2017-03-31 13:13  ming1025  阅读(147)  评论(0编辑  收藏  举报