zoukankan      html  css  js  c++  java
  • 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
    复制代码

    分析

    关系原理图

  • 相关阅读:
    动态规划算法-3
    动态规划算法-2
    动态规划算法-1
    滑动窗口算法-3
    央行副行长提示金融风险:地方偿债高峰期到来
    银行卡换“芯” 更要银行换心
    破解IT运维成本困境,专业化分工是妙方
    php连接mysql
    ajax原生验证用户名是否存在
    ajax跨域问题
  • 原文地址:https://www.cnblogs.com/ming1025/p/6650856.html
Copyright © 2011-2022 走看看