iOS设计模式 - 装饰
iOS设计模式 - 装饰
原理图
说明
1. cocoa框架本身实现了装饰模式(category的方式实现了装饰模式)
2. 装饰模式指的是动态的给一个对象添加一些额外的职责,相对于继承子类来说,装饰模式更加灵活
*3. 本人仅仅实现了最简单的装饰模式,装饰器类是一个具体的类,非抽象类
源码
https://github.com/YouXianMing/iOS-Design-Patterns
//
// GamePlay.h
// DecoratorPattern
//
// Created by YouXianMing on 15/8/1.
// Copyright (c) 2015年 YouXianMing. All rights reserved.
// #import <Foundation/Foundation.h> @interface GamePlay : NSObject /**
* 游戏币的数量
*/
@property (nonatomic) NSInteger coin; /**
* 上下左右的操作
*/
- (void)up;
- (void)down;
- (void)left;
- (void)right; /**
* 选择与开始的操作
*/
- (void)select;
- (void)start; /**
* 按钮 A + B 的操作
*/
- (void)commandA;
- (void)commandB; @end
//
// GamePlay.m
// DecoratorPattern
//
// Created by YouXianMing on 15/8/1.
// Copyright (c) 2015年 YouXianMing. All rights reserved.
// #import "GamePlay.h" @implementation GamePlay - (void)up { NSLog(@"up");
} - (void)down { NSLog(@"down");
} - (void)left { NSLog(@"left");
} - (void)right { NSLog(@"right");
} - (void)select { NSLog(@"select");
} - (void)start { NSLog(@"start");
} - (void)commandA { NSLog(@"commandA");
} - (void)commandB { NSLog(@"commandB");
} @end
//
// DecoratorGamePlay.h
// DecoratorPattern
//
// Created by YouXianMing on 15/8/1.
// Copyright (c) 2015年 YouXianMing. All rights reserved.
// #import <Foundation/Foundation.h>
#import "GamePlay.h" @interface DecoratorGamePlay : NSObject @property (nonatomic) NSInteger coin; - (void)up;
- (void)down;
- (void)left;
- (void)right;
- (void)select;
- (void)start;
- (void)commandA;
- (void)commandB; #pragma mark - 以下为装饰对象新添加的功能 /**
* 剩余几条命
*/
@property (nonatomic, readonly) NSInteger lives; /**
* 作弊 (上下上下左右左右ABAB)
*/
- (void)cheat; @end
//
// DecoratorGamePlay.m
// DecoratorPattern
//
// Created by YouXianMing on 15/8/1.
// Copyright (c) 2015年 YouXianMing. All rights reserved.
// #import "DecoratorGamePlay.h" @interface DecoratorGamePlay () @property (nonatomic, strong) GamePlay *gamePlay; @end @implementation DecoratorGamePlay #pragma mark - 初始化
- (instancetype)init { self = [super init];
if (self) { // 装饰对象包含一个真实对象的引用
self.gamePlay = [GamePlay new];
} return self;
} #pragma mark - 让真实对象的引用执行对应的方法
- (void)up { [_gamePlay up];
} - (void)down { [_gamePlay down];
} - (void)left { [_gamePlay left];
} - (void)right { [_gamePlay right];
} - (void)select { [_gamePlay select];
} - (void)start { [_gamePlay start];
} - (void)commandA { [_gamePlay commandA];
} - (void)commandB { [_gamePlay commandB];
} #pragma mark - 装饰器新添加的方法
- (void)cheat { [_gamePlay up];
[_gamePlay down];
[_gamePlay up];
[_gamePlay down];
[_gamePlay left];
[_gamePlay right];
[_gamePlay left];
[_gamePlay right];
[_gamePlay commandA];
[_gamePlay commandB];
[_gamePlay commandA];
[_gamePlay commandB];
} @synthesize lives = _lives;
- (NSInteger)lives { // 相关处理逻辑
return ;
} @end
分析
以下是装饰模式实现细节对照图
iOS设计模式 - 装饰的更多相关文章
- IOS设计模式之二(门面模式,装饰器模式)
本文原文请见:http://www.raywenderlich.com/46988/ios-design-patterns. 由 @krq_tiger(http://weibo.com/xmuzyq) ...
- IOS设计模式第四篇之装饰设计模式的类别设计模式
装饰设计模式 装饰设计模式动态的添加行为和责任向一个对象而不修改他的任何代码.他是你子类化修改类的行为用通过另一个对象的包装的代替方法. 在Objective-c里面有很多这种设计模式的实现,像cat ...
- IOS设计模式之一(MVC模式,单例模式)
iOS 设计模式-你可能已经听说过这个词,但是你真正理解它意味着什么吗?虽然大多数的开发者可能都会认为设计模式是非常重要的,然而关于设计模式这一主题的文章却不多,并且有时候我们开发者在写代码的时候也不 ...
- iOS 设计模式
很赞的总结 iOS Design Patterns 中文版 IOS设计模式之一(MVC模式,单例模式) IOS设计模式之二(门面模式,装饰器模式) IOS设计模式之三(适配器模式,观察者模式) IOS ...
- Java设计模式——装饰者模式
JAVA 设计模式 装饰者模式 用途 装饰者模式 (Decorator) 动态地给一个对象添加一些额外的职责.就增加功能来说,Decorator 模式相比生成子类更为灵活. 装饰者模式是一种结构式模式 ...
- iOS 设计模式之工厂模式
iOS 设计模式之工厂模式 分类: 设计模式2014-02-10 18:05 11020人阅读 评论(2) 收藏 举报 ios设计模式 工厂模式我的理解是:他就是为了创建对象的 创建对象的时候,我们一 ...
- iOS设计模式之生成器
iOS设计模式之生成器 1.生成器模式的定义 (1): 将一个复杂的对象的构件与它的表示分离,使得相同的构建过程能够创建不同的表示 (2): 生成器模式除了客户之外还包括一个Director(指导者) ...
- IOS设计模式之三:MVC模式
IOS设计模式之三:MVC模式 模型-视图-控制器 这个模式其实应该叫做MCV,用控制器把model与view隔开才对,也就是model与view互相不知道对方的存在,没有任何瓜葛,他们就像一个团 ...
- JAVA设计模式--装饰器模式
装饰器模式 装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构.这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装. 这种模式创建了一个装饰 ...
随机推荐
- Windows Git Bash命令行下创建git仓库并更新到github
大二的时候就听过老师说有一个叫git的版本管理工具,当时只是听老师说说而已,也没有去使用它,因为当时用过svn,就感觉自己没多少东西需要git管理. 最近几天,我经常在开源中国看别人的帖子,看到别人对 ...
- hibernate关联关系的crud之级联
cascade级联,只会影响CRUD的CUD,不会影响读取.不设置级联,从多的一方能读出一的一方,设了级联,从一的一方,默认也不能读出多的一方. 如果两个对象之间有关联,不管是一对多,多对一,单向还是 ...
- redis 常用的server的命令
- 从java到web前端再到php,一路走来的小总结
java的学习: 初学者对Java的学习,上来的感觉都是比较难,感觉java的东西很多,如此多的类和接口.有时还弄不懂为啥实例化出一个int空数组为什么数组中默认都是0,实例化一个空字符串数组时(St ...
- VS2013漂亮字体
使用字体:Fixedsys Excelsior 3.011.首先下载字体:http://www.fixedsysexcelsior.com/ 2.安装字体:控制面板 -> 字体,复制下载的文件进 ...
- android RecyclerView简单的使用
转自:https://blog.csdn.net/lmj623565791/article/details/45059587 概述 RecyclerView出现已经有一段时间了,相信大家肯定不陌生了, ...
- [PHP] PHP的纯CPU基准测试(PHP5.5.9 vs PHP7.2.1)
PHP的纯CPU基准测试(PHP5.5.9 vs PHP7.2.1): 1.bench.php 可在PHP源代码的 php-src/Zend 目录 2.micro_bench.php 也可以在 PHP ...
- OpenGL绘制一个三角形
应该建立一个vertex shader文件和一个pixel shader文件,分别命名为shader.vsh和shader.fsh. shader.vsh: attribute vec3 positi ...
- Java基础教程(8)--表达式、语句和块
一.表达式 表达式由变量和运算符组成.下面是一个简单的赋值表达式: a = 0; 表达式都具有运算结果,因为赋值表达式的运算结果是左侧操作数的值,因此上面的表达式将会返回一个0.可以使用简单的 ...
- apache 优化配置详解
###=========httpd.conf begin===================##Apache主配置文件##设置服务器的基础目录,默认为Apache安装目录ServerRoot &qu ...