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
    复制代码

    分析

    关系原理图

  • 相关阅读:
    使用百度网盘配置私有Git服务
    Linked dylibs built for GC-only but object files built for retain/release for architecture x86_64
    我的博客搬家啦!!!
    今日头条核心业务(高级)开发工程师,直接推给部门经理,HC很多,感兴趣的可以一起聊聊。
    学习Python的三种境界
    拿到阿里,网易游戏,腾讯,smartx的offer的过程
    关于计算机网络一些问题的思考
    网易游戏面试经验(三)
    网易游戏面试经验(二)
    网易游戏面试经验(一)
  • 原文地址:https://www.cnblogs.com/ming1025/p/6650856.html
Copyright © 2011-2022 走看看