美文网首页
iOS设计模式 (三) 工厂模式之工厂方法

iOS设计模式 (三) 工厂模式之工厂方法

作者: handsome5 | 来源:发表于2018-05-12 18:25 被阅读3次
    工厂模式:工厂方法
    • 定义一个用于创建对象的接口,让子类决定实例化哪一个类。工厂方法使一个类的实例化延迟到其子类。
    • 工厂方法UML


      2.png
    demo实现
    • ColorViewGenerator(抽象类)
    #import <Foundation/Foundation.h>
    #import "RedView.h"
    #import "BlueView.h"
    
    @interface ColorViewGenerator : NSObject
    
    - (ColorView *)colorViewWithFrame:(CGRect)rect;
    
    @end
    #import "ColorViewGenerator.h"
    
    @implementation ColorViewGenerator
    
    - (ColorView *)colorViewWithFrame:(CGRect)rect
    {
       return [[ColorView alloc] initWithFrame:rect];
    }
    
    @end
    
    
    • RedViewGenerator(类)
    #import "ColorViewGenerator.h"
    
    @interface RedViewGenerator : ColorViewGenerator
    
    @end
    
    #import "RedViewGenerator.h"
    
    @implementation RedViewGenerator
    
    - (ColorView *)colorViewWithFrame:(CGRect)rect
    {
        return [[RedView alloc] initWithFrame:rect];
    }
    
    @end
    
    
    • BlueViewGenrator(product类)
    #import "ColorViewGenerator.h"
    
    @interface BlueViewGenrator : ColorViewGenerator
    
    @end
    #import "BlueViewGenrator.h"
    
    @implementation BlueViewGenrator
    
    - (ColorView *)colorViewWithFrame:(CGRect)rect
    {
        return [[BlueView alloc] initWithFrame:rect];
    }
    
    @end
    
    
    • ColorView 类(product 类)
    #import <UIKit/UIKit.h>
    
    @interface ColorView : UIView
    
    @end
    
    #import "ColorView.h"
    
    @implementation ColorView
    
    - (instancetype)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            self.backgroundColor = [UIColor grayColor];
        }
        return self;
    }
    @end
    
    • RedView 类(子类)
    #import "ColorView.h"
    
    @interface RedView : ColorView
    
    @end
    #import "RedView.h"
    
    @implementation RedView
    
    - (instancetype)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            self.backgroundColor = [UIColor redColor];
            UIImage *backGroundImage = [UIImage imageNamed:@"2"];
            UIImageView *backImageView = [[UIImageView alloc] initWithImage:backGroundImage];
            [self addSubview: backImageView];
        }
        return self;
    }
    
    @end
    
    • BlueView 子类
    #import "ColorView.h"
    
    @interface BlueView : ColorView
    
    @end
    
    #import "BlueView.h"
    
    @implementation BlueView
    
    - (instancetype)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            self.backgroundColor = [UIColor blueColor];
            UIImage *backGroundImage = [UIImage imageNamed:@"3"];
            UIImageView *backImageView = [[UIImageView alloc] initWithImage:backGroundImage];
            [self addSubview: backImageView];
        }
        return self;
    }
    @end
    
    • ViewController 类
    #import <UIKit/UIKit.h>
    
    @interface ViewController : UIViewController
    @end
    #import "ViewController.h"
    #import "BlueViewGenrator.h"
    #import "RedViewGenerator.h"
    #import "RedView.h"
    #import "BlueView.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        RedView *redview = (RedView *)[[RedViewGenerator alloc] colorViewWithFrame:self.view.bounds];
        [self.view addSubview: redview];
        
        BlueView *blueView = (BlueView *) [[BlueViewGenrator alloc] colorViewWithFrame:self.view.bounds];
        [self.view addSubview: blueView];
        
    }
    
    @end
    
    源代码详见:https://github.com/defuliu/FactoryPatterns.git

    相关文章

      网友评论

          本文标题:iOS设计模式 (三) 工厂模式之工厂方法

          本文链接:https://www.haomeiwen.com/subject/rwdkhftx.html