码迷,mamicode.com
首页 > 移动开发 > 详细

iOS设计模式 - 抽象工厂

时间:2015-08-02 07:47:48      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:

iOS设计模式 - 抽象工厂

技术分享

 

原理图

技术分享

 

说明

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

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

 

源码

https://github.com/YouXianMing/AbstractFactoryPattern

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

 

分析

关系原理图

技术分享

 

iOS设计模式 - 抽象工厂

标签:

原文地址:http://www.cnblogs.com/YouXianMing/p/4695045.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!