美文网首页
IOS-工厂模式-Demo

IOS-工厂模式-Demo

作者: 西门淋雨 | 来源:发表于2018-09-03 11:01 被阅读26次

    案例描述:一个食品加工厂,不同的车间生产不同的食物。

    工厂Controller:
    #import <UIKit/UIKit.h>
    
    @interface FactoryVC : UIViewController
    
    @end
    
    #import "FactoryVC.h"
    #import "Food.h"
    #import "UIUtil.h"
    #import "Masonry.h"
    #import "Factory.h"
    @interface FactoryVC ()
    
    @end
    
    @implementation FactoryVC
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.view.backgroundColor = [UIColor whiteColor];
        UIButton *btn1 = [UIUtil btnWithTitle:@"馒头" bgColor:[UIColor lightGrayColor] titleFont:[UIFont systemFontOfSize:13] titleColor:[UIColor blackColor] selecor:@selector(touAction:) target:self];
        btn1.tag = 100;
        [self.view addSubview:btn1];
        __weak typeof(self) weakSelf = self;
        [btn1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(weakSelf.view);
            make.top.equalTo(weakSelf.view).mas_offset(100);
            make.width.mas_equalTo(100);
            make.height.mas_equalTo(40);
        }];
        UIButton *btn2 = [UIUtil btnWithTitle:@"油条" bgColor:[UIColor lightGrayColor] titleFont:[UIFont systemFontOfSize:13] titleColor:[UIColor blackColor] selecor:@selector(touAction:) target:self];
        btn2.tag = 200;
        [self.view addSubview:btn2];
        [btn2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(weakSelf.view);
            make.top.equalTo(btn1.mas_bottom).mas_offset(50);
            make.width.mas_equalTo(100);
            make.height.mas_equalTo(40);
        }];
        
    }
    - (void)touAction:(UIButton *)sender{
        BreakfastType type;
        switch (sender.tag) {
            case 100:
                type = BreakfastType_mantou;
                break;
            case 200:
                type = BreakfastType_youtiao;
                break;
            default:
                type = BreakfastType_mantou;
                break;
        }
        Food *brakfast = [Factory creatBreakfast:type];
        NSLog(@"%@",[brakfast productName]);
        
        UIAlertController *alertCon = [UIAlertController alertControllerWithTitle:@"哈哈" message:[brakfast productName] preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"ok" style:UIAlertActionStyleDefault handler:nil];
        [alertCon addAction:okAction];
        [self presentViewController:alertCon animated:YES completion:nil];
    }
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
    }
    
    @end
    

    工厂:

    #import <Foundation/Foundation.h>
    #import "Food.h"
    typedef NS_ENUM(NSInteger,BreakfastType){
        BreakfastType_mantou,
        BreakfastType_youtiao
    };
    @interface Factory : NSObject
    + (Food *)creatBreakfast:(BreakfastType)type;
    @end
    
    #import "Factory.h"
    #import "MantouBreakfast.h"
    #import "YoutiaoBreakfast.h"
    
    @implementation Factory
    + (Food *)creatBreakfast:(BreakfastType)type{
        Food *breakfast;
        switch (type) {
            case BreakfastType_mantou:
            {
                breakfast = [[MantouBreakfast alloc] init];
            }
                break;
            case BreakfastType_youtiao:
            {
                breakfast = [[YoutiaoBreakfast alloc] init];
            }
                break;
            default:
                return nil;
                break;
        }
        return breakfast;
    }
    @end
    
    

    食物基类

    #import <Foundation/Foundation.h>
    
    @interface Food : NSObject
    - (NSString *)productName;
    @end
    
    
    #import "Food.h"
    
    @implementation Food
    - (NSString *)productName{
        return @"我是早餐";
    }
    @end
    

    馒头

    #import <Foundation/Foundation.h>
    #import "Food.h"
    @interface MantouBreakfast : Food
    
    @end
    
    #import "MantouBreakfast.h"
    
    @implementation MantouBreakfast
    - (NSString *)productName{
        return @"我是馒头==";
    }
    @end
    

    油条

    #import <Foundation/Foundation.h>
    #import "Food.h"
    
    @interface YoutiaoBreakfast : Food
    
    @end
    
    
    #import "YoutiaoBreakfast.h"
    
    @implementation YoutiaoBreakfast
    - (NSString *)productName{
        return @"我是油条==";
    }
    @end
    
    

    相关文章

      网友评论

          本文标题:IOS-工厂模式-Demo

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