ios开发之再谈设计模式
子曰:设计模式这东西,没有好坏之分,只有合适于不合适
天去面试很有意思,技术考官指着最后一道二选一的编程题说,这是昨天晚上专门为你新加的.当时我听后倍感惭愧. 虽然当时在纸上把大概思路和设计说了下.为了感谢主考官的重视程度.我再电脑上把这个设计敲出来实现出来.
题目大概是这个意思: 一个咖啡店卖好几种咖啡:摩卡,布列夫,拿铁等等 咖啡有很多搭配:方糖,鲜牛奶,奶油,盐等. 试设计计算出咖啡(+搭配)的单价模型.
下面来谈谈我的想法
一:虚基类 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开发之浅谈MVVM的架构设计与团队协作
今天写这篇博客是想达到抛砖引玉的作用,想与大家交流一下思想,相互学习,博文中有不足之处还望大家批评指正.本篇博客的内容沿袭以往博客的风格,也是以干货为主,偶尔扯扯咸蛋(哈哈~不好好工作又开始发表博客啦 ...
- iOS开发之单例设计模式(完整正确版本)
单例的意思从字面上就可以略知一二,所谓单例就是确保在程序运行过程中只创建一个对象实例.可以用于需要被多次广泛或者说多次使用的资源中,比如我们常见的网络请求类.工具类以及其它管理类等.比如我iOS开发中 ...
- iOS开发之再探多线程编程:Grand Central Dispatch详解
Swift3.0相关代码已在github上更新.之前关于iOS开发多线程的内容发布过一篇博客,其中介绍了NSThread.操作队列以及GCD,介绍的不够深入.今天就以GCD为主题来全面的总结一下GCD ...
- iOS 开发中常见的设计模式
最近有小伙伴问到在iOS开发中的几种设计模式,这里摘录一下别人的总结(因为已经感觉总结得差不多了,适用的可以阅读一下) 首先是开发中的23中设计模式分为三大类:1.创建型 2.结构型 3.行为型 (i ...
- iOS开发中的MVC设计模式
我们今天谈谈cocoa程序设计中的 模型-视图-控制器(MVC)范型.我们将从两大方面来讨论MVC: 什么是MVC? M.V.C之间的交流方式是什么样子的? 理解了MVC的概念,对cocoa程序开发是 ...
- iOS开发中常用的设计模式
常用的设计模式(一)代理模式应用场景:当一个类的某些功能需要由别的类来实现,但是又不确定具体会是哪个类实现.优势:解耦合敏捷原则:开放-封闭原则实例:tableview的 数据源delegate,通过 ...
- Windows phone应用开发[22]-再谈下拉刷新
几周之前在博客更新一篇Windows phone应用开发[18]-下拉刷新 博文,有很多人在微博和博客评论中提到了很多问题.其实在实际项目中我基于这篇博文提出解决问题思路优化了这个解决方案.为了能够详 ...
- iOS开发--Swift 基于MVC设计模式的简单的tableViewDemo
如果说MVC是最好的设计模式, 可能很多人并不赞同, 但是如果说MVC是最主流, 应用面最广的设计模式, 我想这是毫无争议的. 不说废话, 直接演示在Swift中如何使用MVC新建工程(我并没有新建文 ...
- ios开发中的基本设计模式
(一)代理模式应用场景:当一个类的某些功能需要由别的类来实现,但是又不确定具体会是哪个类实现.优势:解耦合敏捷原则:开放-封闭原则实例:tableview的 数据源delegate,通过和protoc ...
随机推荐
- Android源码下载
Android源码下载 1.安装git 2.安装repo 从这里 https://dl-ssl.google.com/dl/googlesource/git-repo/repo 下载repo文件 3. ...
- Protel在PCB中添加汉字
使用Protel 99SE的工程人员都知道Protel在PCB绘制中是不支持汉字输入的,但作为工厂生产调试的方便,不可避免的要在PCB上制作中文标示,有时为说明板子的用途,注意事项等都要输入中文丝印, ...
- c++学习笔记(c++中的引用)
1.c++中的bool类型: 其实c语言中也有bool类型,如果是遵守c90标准的编译器(其实现在大量编译器都是c90标准的),对于bool类型的使用除了要使用头文件 stdbool.h外,与 ...
- MSSQL 日期操作函数 总结
set ANSI_NULLS ON set QUOTED_IDENTIFIER ON go ALTER FUNCTION [dbo].[ufn_getDateOfWeek] (@Date Dateti ...
- Spring、Hello AOP
AOP 概念:http://blog.csdn.net/moreevan/article/details/11977115 AOP 所使用到的jar 包: aspectjrt.jar common-a ...
- 使用扩展名获取mimetype
在Android中很多时候我们需要计算出文件的mimetype,而我们通常的思路就是通过扩展名来获取对应的mimetype,而如果自行处理,将维护一个比较大的映射表,而实际上大可不必,Android提 ...
- C# 4.0 新特性-dynamic 【转】
前段时间看过一些关于dynamic这个C#4中的新特性,看到有些朋友认为dynamic的弊大于利,如无法使用编译器智能提示,无法在编译时做静态类型检查,性能差等等.因此在这篇文章中我将就这些问题来对d ...
- C# webservice开发
一.Webservice简介Web Service也叫XML Web Service. Web Service是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求,轻量 ...
- CSS或者JS实现鼠标悬停显示另一元素
想达到鼠标悬停到元素a上,显示另一个元素b,可以通过css实现也可以通过js实现.js:写两个函数:mouseenter,mouseleave,例如:其中 $("#a").mous ...
- javascript实现倒计时
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...