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设计模式 - 装饰的更多相关文章

  1. IOS设计模式之二(门面模式,装饰器模式)

    本文原文请见:http://www.raywenderlich.com/46988/ios-design-patterns. 由 @krq_tiger(http://weibo.com/xmuzyq) ...

  2. IOS设计模式第四篇之装饰设计模式的类别设计模式

    装饰设计模式 装饰设计模式动态的添加行为和责任向一个对象而不修改他的任何代码.他是你子类化修改类的行为用通过另一个对象的包装的代替方法. 在Objective-c里面有很多这种设计模式的实现,像cat ...

  3. IOS设计模式之一(MVC模式,单例模式)

    iOS 设计模式-你可能已经听说过这个词,但是你真正理解它意味着什么吗?虽然大多数的开发者可能都会认为设计模式是非常重要的,然而关于设计模式这一主题的文章却不多,并且有时候我们开发者在写代码的时候也不 ...

  4. iOS 设计模式

    很赞的总结 iOS Design Patterns 中文版 IOS设计模式之一(MVC模式,单例模式) IOS设计模式之二(门面模式,装饰器模式) IOS设计模式之三(适配器模式,观察者模式) IOS ...

  5. Java设计模式——装饰者模式

    JAVA 设计模式 装饰者模式 用途 装饰者模式 (Decorator) 动态地给一个对象添加一些额外的职责.就增加功能来说,Decorator 模式相比生成子类更为灵活. 装饰者模式是一种结构式模式 ...

  6. iOS 设计模式之工厂模式

    iOS 设计模式之工厂模式 分类: 设计模式2014-02-10 18:05 11020人阅读 评论(2) 收藏 举报 ios设计模式 工厂模式我的理解是:他就是为了创建对象的 创建对象的时候,我们一 ...

  7. iOS设计模式之生成器

    iOS设计模式之生成器 1.生成器模式的定义 (1): 将一个复杂的对象的构件与它的表示分离,使得相同的构建过程能够创建不同的表示 (2): 生成器模式除了客户之外还包括一个Director(指导者) ...

  8. IOS设计模式之三:MVC模式

    IOS设计模式之三:MVC模式   模型-视图-控制器 这个模式其实应该叫做MCV,用控制器把model与view隔开才对,也就是model与view互相不知道对方的存在,没有任何瓜葛,他们就像一个团 ...

  9. JAVA设计模式--装饰器模式

    装饰器模式 装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构.这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装. 这种模式创建了一个装饰 ...

随机推荐

  1. Eclipse 安装spring插件spring tool suite(STS)

    安装方法有2种,一种是在线安装,比较方便,但耗时较长,一种是离线安装,步骤复杂一些,但耗时少,下面请看详细步骤. 方法一:在线安装     1:打开eclipse菜单Help>Eclise Ma ...

  2. EasyMock set方法报错: java.lang.AssertionError

    有返回值的方法没问题, 直接andReturn就行了. EasyMock.expect(info.getWebTitle()).andReturn(StringUtils.EMPTY).anyTime ...

  3. ES5支持的方法

    ES5的支持的方法 concat() 把元素衔接到数组中. every() 测试断言函数是否对每个数组元素都为真 filter() 返回满足断言函数的数组元素 forEach() 为数组的每一个元素调 ...

  4. jstl fmt标签的使用

    所有标签 fmt:requestEncoding fmt:setLocale fmt:timeZone fmt:setTimeZone fmt:bundle fmt:setBundle fmt:mes ...

  5. springboot-21-maven多环境打包

    前几天项目需要用到分环境打包, 于是研究了下, 由于项目基于springboot的, 所以分两个情况进行说明: 1), springboot的多环境配置 2), maven-springboot的多环 ...

  6. 14 线程间协作的两种方式:wait、notify、notifyAll和Condition

    原文链接:http://www.cnblogs.com/dolphin0520/p/3920385.html 在前面我们将了很多关于同步的问题,然而在现实中,需要线程之间的协作.比如说最经典的生产者- ...

  7. SQL 常用脚本,非常适用

    一.基础 1.说明:创建数据库 CREATE DATABASE database-name 2.说明:删除数据库 drop database dbname 3.说明:备份sql server --- ...

  8. Comet事件分析

    简介[ Introduction ]  使用APR或者NIO API作为连接器的基础,Tomcat能够提供一些在阻塞IO之上的有效扩展,用于支持Servlet API. [ With usage of ...

  9. <数据挖掘导论>读书笔记11异常检测

    异常检测的目标是发现与大部分其他对象不同的对象.通常,异常对象被称作离群点(Outlier). 异常检测也称偏差检测(Deviation detection),因为异常对象的属性值明显偏离期望的或者常 ...

  10. 【LeetCode题解】94_二叉树的中序遍历

    目录 [LeetCode题解]94_二叉树的中序遍历 描述 方法一:递归 Java 代码 Python代码 方法二:非递归 Java 代码 Python 代码 [LeetCode题解]94_二叉树的中 ...