美文网首页
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

    案例描述:一个食品加工厂,不同的车间生产不同的食物。 工厂Controller: 工厂: 食物基类 馒头 油条

  • iOS-工厂模式

    工厂模式的特点是,根据客户的要求,创建出不同的产品(也就是实例对象),而客户不需要知道产生的细节。 一、简单工厂模...

  • 创建对象的几种模式和一个事件管理器实现

    构造函数模式、混合模式、模块模式、工厂模式、单例模式、发布订阅模式的范例 . . . . . 具体实现 Demo[...

  • 设计模式

    认识过程 仰视 简单 第一版demo 第二版demo + 思考 + 使用 遇到过的模式: spring 的工厂模式...

  • 设计模式《工厂方法模式》

    引言   上一节我们说了单例模式,这一节我们来说说工厂方法模式。 示例地址   Demo 先看类图 工厂方法模式定...

  • 抽象工厂模式Demo讲解

    0.背景 小明是一位艺术总监,他准备开一些艺术工厂(factory),但是由于资金问题,这些艺术工厂暂时只做一件事...

  • Cell工厂设计

    Cell工厂设计 关于一个Cell工厂设计模式的 Demo Model层 .首先建立 BaseModel并继承他建...

  • 工厂模式及抽象工厂模式简介

    本文介绍了工厂模式,抽象工厂模式并附有java代码实现demo。内容仅供参考使用,有不足之处请及时指出,也欢迎大家...

  • 简单工厂模式

    工厂模式 简单例子Demo 创建一个 接口类Animals.javapublic interface Animal...

  • spring 设计模式篇—工厂模式

    一、解决了什么问题? 二、工厂模式演变过程,及代码实现demo 三、源-简单工厂 Car.java BmCar.j...

网友评论

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

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