美文网首页
从iOS理解工厂和抽象工厂

从iOS理解工厂和抽象工厂

作者: Simple_Code | 来源:发表于2020-08-14 18:02 被阅读0次
    image.png

    如上图所示,外面红色大框是抽象工厂,里面小框是工厂。

    工厂

    工厂:我们在创建对象时不会对客户端暴露创建逻辑,并且是通过使用一个共同的接口来指向新创建的对象。

    工厂代码演示:

    ---------------XiaoMiPhone---------------
    #import <Foundation/Foundation.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface XiaoMiPhone : NSObject
    - (void)create;
    @end
    
    NS_ASSUME_NONNULL_END
    
    #import "XiaoMiPhone.h"
    
    @implementation XiaoMiPhone
    - (void)create {
        NSLog(@"create-XiaoMiPhone");
    }
    @end
    
    ---------------RedMiPhone---------------
    #import <Foundation/Foundation.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface RedMiPhone : NSObject
    - (void)create;
    @end
    
    NS_ASSUME_NONNULL_END
    
    #import "RedMiPhone.h"
    
    @implementation RedMiPhone
    - (void)create {
        NSLog(@"create-RedMiPhone");
    }
    @end
    
    #import <Foundation/Foundation.h>
    
    typedef NS_ENUM(NSInteger,PhoneFactoryType) {
        PhoneTypeXiaoMi = 0,
        PhoneTypeRedMi,
    };
    
    ---------------XiaoMiPhoneFactory----------------
    NS_ASSUME_NONNULL_BEGIN
    
    @interface XiaoMiPhoneFactory : NSObject
    + (id)createPhone:(PhoneFactoryType)type;
    @end
    
    NS_ASSUME_NONNULL_END
    
    #import "XiaoMiPhoneFactory.h"
    #import "XiaoMiPhone.h"
    #import "RedMiPhone.h"
    
    @implementation XiaoMiPhoneFactory
    + (id)createPhone:(PhoneFactoryType)type {
        switch (type) {
            case PhoneTypeXiaoMi:
            {
                return [XiaoMiPhone new];
            }
                break;
            case PhoneTypeRedMi:
            {
                return [RedMiPhone new];
            }
                break;
            default:
                break;
        }
    }
    @end
    
    使用:
      XiaoMiPhone *xPhone = [XiaoMiPhoneFactory createPhone:PhoneTypeXiaoMi];
      RedMiPhone *rPhone = [XiaoMiPhoneFactory createPhone:PhoneTypeRedMi];
        [xPhone create];
        [rPhone create];
    

    抽象工厂

    抽象工厂:是围绕一个超级工厂创建其他工厂。该超级工厂又称为其他工厂的工厂。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。
    工厂代码演示:

    ---------------XiaoMiTvFactory---------------
    #import <Foundation/Foundation.h>
    typedef NS_ENUM(NSInteger,TvFactoryType) {
        TvType4A = 0,
        TvType5A,
    };
    NS_ASSUME_NONNULL_BEGIN
    
    @interface XiaoMiTvFactory : NSObject
    + (id)createPhone:(TvFactoryType)type;
    @end
    
    NS_ASSUME_NONNULL_END
    
    #import "XiaoMiTvFactory.h"
    #import "XiaoMi4ATv.h"
    #import "XiaoMi5ATv.h"
    
    @implementation XiaoMiTvFactory
    + (id)createPhone:(TvFactoryType)type {
        switch (type) {
            case TvType4A:
            {
                return [XiaoMi4ATv new];
            }
                break;
            case TvType5A:
            {
                return [XiaoMi5ATv new];
            }
                break;
            default:
                break;
        }
    }
    @end
    
    ---------------XiaoMi5ATv---------------
    #import <Foundation/Foundation.h>
    NS_ASSUME_NONNULL_BEGIN
    
    @interface XiaoMi5ATv : NSObject
    - (void)create;
    @end
    
    NS_ASSUME_NONNULL_END
    
    #import "XiaoMi5ATv.h"
    @implementation XiaoMi5ATv
    - (void)create {
        NSLog(@"create-XiaoMi5ATv");
    }
    @end
    
    ---------------XiaoMi4ATv---------------
    #import <Foundation/Foundation.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface XiaoMi4ATv : NSObject
    - (void)create;
    @end
    
    NS_ASSUME_NONNULL_END
    
    #import "XiaoMi4ATv.h"
    
    @implementation XiaoMi4ATv
    - (void)create {
        NSLog(@"create-XiaoMi4ATv");
    }
    @end
    
    ---------------XiaoMiFactory---------------
    #import <Foundation/Foundation.h>
    typedef NS_ENUM(NSInteger,XiaoMiFactoryType) {
        XiaoMiTypePhone = 0,
        XiaoMiTypeTv,
    };
    NS_ASSUME_NONNULL_BEGIN
    
    @interface XiaoMiFactory : NSObject
    + (Class)createFactory:(XiaoMiFactoryType)type;
    @end
    
    NS_ASSUME_NONNULL_END
    
    #import "XiaoMiFactory.h"
    #import "XiaoMiPhoneFactory.h"
    #import "XiaoMiTvFactory.h"
    
    @implementation XiaoMiFactory
    + (Class)createFactory:(XiaoMiFactoryType)type {
        switch (type) {
            case XiaoMiTypePhone:
            {
                return [XiaoMiPhoneFactory class];
            }
                break;
            case XiaoMiTypeTv:
            {
                return [XiaoMiTvFactory class];
            }
                break;
            default:
                break;
        };
    }
    

    相关文章

      网友评论

          本文标题:从iOS理解工厂和抽象工厂

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