ios开发之再谈设计模式
2013-09-18 19:15
295 查看
子曰:设计模式这东西,没有好坏之分,只有合适于不合适
天去面试很有意思,技术考官指着最后一道二选一的编程题说,这是昨天晚上专门为你新加的.当时我听后倍感惭愧. 虽然当时在纸上把大概思路和设计说了下.为了感谢主考官的重视程度.我再电脑上把这个设计敲出来实现出来.
题目大概是这个意思: 一个咖啡店卖好几种咖啡:摩卡,布列夫,拿铁等等 咖啡有很多搭配:方糖,鲜牛奶,奶油,盐等. 试设计计算出咖啡(+搭配)的单价模型.
下面来谈谈我的想法
这个类包含什么呢
{
. 咖啡的单价(不含 方糖 奶等 调味料) —>price
. 一个存放调味料的容器 —>ecoratorRelishChain
. 一个可以得到总价的方法 —> getTotalPrices
}
下面是代码
基类咖啡.h
基类咖啡.m
这个类包含什么呢
{
. 自身的单价 —>price
. 可以修改价钱的策略 —> configPrivilege
}
方糖 等抽象出来的基类EcoratorRelish.h
EcoratorRelish.m
DecoratorChain.m
下面的实现代码大家应该都会写了吧. 其实回头看下 中间应用到的设计模式 最明显的是 装饰 和 组合 策略
天去面试很有意思,技术考官指着最后一道二选一的编程题说,这是昨天晚上专门为你新加的.当时我听后倍感惭愧. 虽然当时在纸上把大概思路和设计说了下.为了感谢主考官的重视程度.我再电脑上把这个设计敲出来实现出来.
题目大概是这个意思: 一个咖啡店卖好几种咖啡:摩卡,布列夫,拿铁等等 咖啡有很多搭配:方糖,鲜牛奶,奶油,盐等. 试设计计算出咖啡(+搭配)的单价模型.
下面来谈谈我的想法
一:虚基类 Coffee
首先 我抽象出了一个虚基类 Coffee, 什么摩卡 布列夫 拿铁都继承这个类这个类包含什么呢
{
. 咖啡的单价(不含 方糖 奶等 调味料) —>price
. 一个存放调味料的容器 —>ecoratorRelishChain
. 一个可以得到总价的方法 —> getTotalPrices
}
下面是代码
基类咖啡.h
//abstract 咖啡基类 @class DecoratorChain; @interface Coffee: NSObject @property ( nonatomic,strong ) DecoratorChain *ecoratorRelishChain;//用来存储 奶 方糖 等等的调料 可以把它想象成一个调味盒 @property ( nonatomic,strong ) NSDecimalNumber *price;//单价 不含配料 -(NSDecimalNumber *) getTotalPrices;//得到总价 @end
基类咖啡.m
@interface Coffee() @property(nonatomic,strong) NSString * _coffeeName; @end @implementation Coffee @synthesize _coffeeName,price,ecoratorRelishChain; - (id)init { self = [super init]; if (self) { _coffeeName=@"咖啡名称"; price = [[NSDecimalNumber alloc] initWithString:@"20"]; } return self; } -(NSDecimalNumber *)getTotalPrices { return [self.price decimalNumberByAdding: [ecoratorRelishChain getCountPrice]]; } @end
二: 虚基类 EcoratorRelish
EcoratorRelish 是 方糖 奶油 牛奶 盐 等等的抽象类 这个继承在button 点击的时候 可以自动将自己加入到chain (调料盘中)这个类包含什么呢
{
. 自身的单价 —>price
. 可以修改价钱的策略 —> configPrivilege
}
方糖 等抽象出来的基类EcoratorRelish.h
@interface EcoratorRelish: UIButton @property(nonatomic,strong) NSDecimalNumber *price;//单价 //Overload -(void)configPrivilege;//可以配置优惠策略 -(DecoratorChain *)getComponentCoffee; @end
EcoratorRelish.m
@implementation EcoratorRelish @synthesize price; - (id)init { self = [super init]; if (self) { [self addTarget:self action:@selector(addDecoratorChain) forControlEvents:UIControlEventTouchUpInside]; } return self; } //Overload -(void)configPrivilege { //可以配置优惠策略 } //获取当前的辅料坐在的ViewController - (UIViewController *)getViewController { Class vcc = [UIViewController class]; UIResponder *responder = self; while ((responder = [responder nextResponder])) if ([responder isKindOfClass: vcc]) return (UIViewController *)responder; return nil; } //获取要装饰的咖啡的调味盒(chain) -(DecoratorChain *)getEcoratorRelishChain { return [self getViewController].coffee.ecoratorRelishChain; } //将自己加到咖啡的调味盒(chain) -(void)addDecoratorChain { [[self getEcoratorRelishChain] addDecoratorRelish:self]; } @end
三: 调料盒 DecoratorChain
DecoratorChain.h//用于保存配料的chain @interface DecoratorChain: NSMutableArray -(void)addDecoratorRelish:(EcoratorRelish*)ecoratorRelish; @end
DecoratorChain.m
@interface DecoratorChain() @property(nonatomic,strong) NSDecimalNumber * _countPrice; @end @implementation DecoratorChain @synthesize _countPrice; - (id)init { self = [super init]; if (self) { _countPrice = [[NSDecimalNumber alloc]init]; } return self; } -(void)addDecoratorRelish:(EcoratorRelish*)ecoratorRelish { [self addObject:ecoratorRelish]; } //得到当前所有chain 里面的总价 -(NSDecimalNumber*)getCountPrice { for (EcoratorRelish *tmp in self ) { [_countPrice decimalNumberByAdding:tmp.price]; } return _countPrice; } @end
下面的实现代码大家应该都会写了吧. 其实回头看下 中间应用到的设计模式 最明显的是 装饰 和 组合 策略
相关文章推荐
- IOS开发中的几种设计模式介绍
- ios 开发 设计模式之---策略模式
- ios开发之代理设计模式
- iOS应用设计模式开发中职责链(责任链)模式的实现解析
- iOS开发中的设计模式简单工厂模式
- iOS 开发中常见的设计模式
- IOS开发中的几种设计模式介绍
- IOS开发中的几种设计模式介绍
- ios项目总结一:开发中常用的设计模式
- 【ios开发技术】Swift 语言深入--Swift设计模式 (2/2)
- iOS开发中的几种设计模式
- iOS开发设计模式之代理
- iphone(ios开发)之23种设计模式简介
- iOS开发-设计模式
- ios 开发(二)设计模式
- 深入解析iOS应用开发中对设计模式中的桥接模式的使用
- IOS开发中的几种设计模式介绍
- iOS开发中的21种设计模式
- 详解iOS应用开发中使用设计模式中的抽象工厂模式
- 解析iOS应用开发中对设计模式中的抽象工厂模式的实现