子曰:设计模式这东西,没有好坏之分,只有合适于不合适

天去面试很有意思,技术考官指着最后一道二选一的编程题说,这是昨天晚上专门为你新加的.当时我听后倍感惭愧. 虽然当时在纸上把大概思路和设计说了下.为了感谢主考官的重视程度.我再电脑上把这个设计敲出来实现出来.
题目大概是这个意思: 一个咖啡店卖好几种咖啡:摩卡,布列夫,拿铁等等 咖啡有很多搭配:方糖,鲜牛奶,奶油,盐等. 试设计计算出咖啡(+搭配)的单价模型.

下面来谈谈我的想法

一:虚基类 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开发之再谈设计模式的更多相关文章

  1. iOS开发之浅谈MVVM的架构设计与团队协作

    今天写这篇博客是想达到抛砖引玉的作用,想与大家交流一下思想,相互学习,博文中有不足之处还望大家批评指正.本篇博客的内容沿袭以往博客的风格,也是以干货为主,偶尔扯扯咸蛋(哈哈~不好好工作又开始发表博客啦 ...

  2. iOS开发之单例设计模式(完整正确版本)

    单例的意思从字面上就可以略知一二,所谓单例就是确保在程序运行过程中只创建一个对象实例.可以用于需要被多次广泛或者说多次使用的资源中,比如我们常见的网络请求类.工具类以及其它管理类等.比如我iOS开发中 ...

  3. iOS开发之再探多线程编程:Grand Central Dispatch详解

    Swift3.0相关代码已在github上更新.之前关于iOS开发多线程的内容发布过一篇博客,其中介绍了NSThread.操作队列以及GCD,介绍的不够深入.今天就以GCD为主题来全面的总结一下GCD ...

  4. iOS 开发中常见的设计模式

    最近有小伙伴问到在iOS开发中的几种设计模式,这里摘录一下别人的总结(因为已经感觉总结得差不多了,适用的可以阅读一下) 首先是开发中的23中设计模式分为三大类:1.创建型 2.结构型 3.行为型 (i ...

  5. iOS开发中的MVC设计模式

    我们今天谈谈cocoa程序设计中的 模型-视图-控制器(MVC)范型.我们将从两大方面来讨论MVC: 什么是MVC? M.V.C之间的交流方式是什么样子的? 理解了MVC的概念,对cocoa程序开发是 ...

  6. iOS开发中常用的设计模式

    常用的设计模式(一)代理模式应用场景:当一个类的某些功能需要由别的类来实现,但是又不确定具体会是哪个类实现.优势:解耦合敏捷原则:开放-封闭原则实例:tableview的 数据源delegate,通过 ...

  7. Windows phone应用开发[22]-再谈下拉刷新

    几周之前在博客更新一篇Windows phone应用开发[18]-下拉刷新 博文,有很多人在微博和博客评论中提到了很多问题.其实在实际项目中我基于这篇博文提出解决问题思路优化了这个解决方案.为了能够详 ...

  8. iOS开发--Swift 基于MVC设计模式的简单的tableViewDemo

    如果说MVC是最好的设计模式, 可能很多人并不赞同, 但是如果说MVC是最主流, 应用面最广的设计模式, 我想这是毫无争议的. 不说废话, 直接演示在Swift中如何使用MVC新建工程(我并没有新建文 ...

  9. ios开发中的基本设计模式

    (一)代理模式应用场景:当一个类的某些功能需要由别的类来实现,但是又不确定具体会是哪个类实现.优势:解耦合敏捷原则:开放-封闭原则实例:tableview的 数据源delegate,通过和protoc ...

随机推荐

  1. win7下设置 WiFi AP

    开启windows 7的隐藏功能:虚拟WiFi和SoftAP(即虚拟无线AP),就可以让计算机变成无线路由器.实现共享上网. 1.以管理员身份运行命令提示符: “开始”---在搜索栏输入“cmd”-- ...

  2. Oracle EBS-SQL (MRP-1):检查期间内计划完成的任务.sql

    /*期间内车间任务下达记录数不包含配件任务*/ select     WE.DESCRIPTION                                                任务说 ...

  3. PTF在PET上印刷線路的注意事項

    PTF: Polymer Thick Film (聚合厚模),維基的解釋 PET: Polyethylene terephthalate (聚乙烯對苯二甲酸酯),維基的解釋 就如同大家所知道的,相較於 ...

  4. editplus使用:非法字符: \65279

    众所周知,在跨程序的工程中,统一编码是至关重要的,而目前最普遍的则是统一采用“utf8”编码方案. 但是在采用utf8方案的时候,请注意编辑器的自作聪明. 比如editplus. 原因就在于某些编辑器 ...

  5. 开发汉澳即时通信网,2006年上线,QQ死期到了

    为汉澳sinox用户打造即时通信网让大家用上即时通信软件 近期腾讯关闭了linuxQQ登录,汉澳 sinox也登陆不上.非windows用户再也不能用上即时通信软件了! 这是多么可悲的事,可是我们必须 ...

  6. unity3d实现Socket

    首先创建一个服务器 using UnityEngine; using System.Collections; using System.Net.Sockets; using System.Net; u ...

  7. MFC CListCtrl得到ctrl,shift多选的行号

    vector<int> selVect; int count = m_consumeList.GetItemCount(); //你的列表多少行 for (int i = 0; i< ...

  8. leetcode Swap Nodes in Pairs python

    # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = ...

  9. Linux学习之echo命令

    语法: # echo [Options] [String] 方括号中的项目是可选的.字符串可以定义为字符的有限序列(如字母,数字,符号,标点符号). 当echo命令不带任何选项或字符串使用时,它会在显 ...

  10. Highcharts使用手册

    chart: { type: 'area', ignoreHiddenSeries: false, //如果true,一旦一个系列被隐藏,轴将会扩展剩余的可见系列 }, 这是设置的两个纵坐标轴: yA ...