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. vs2010 编译 boost1.65.1

    vs2010编译boos1.65.1注意选项 vs2010 不支持 c99 ,string.c和debugger.c 变量定义顺序位置报错,改变顺序即可. debugger.c va_copy 在c8 ...

  2. 安装win7 64位和win10 64位双系统小结

    1.gpt比mbr更先进.与主启动记录 (MBR) 分区方法相比,GPT 具有更多的优点,因为它允许每个磁盘有多达 128 个分区(mbr只支持4个分区),支持高达 18 千兆兆字节的卷大小,允许将主 ...

  3. ele

    vue饿了么app项目实战视频 5-1 1.项目代码规范修改.

  4. js中请求数据的$post和$ajax区别(同步和异步问题)

    $.post和$.Ajax都为页面上向后台发送请求,请求数据 1.post 因为post默认为异步请求,可是有时候我们会发现,本来要求请求马上出现,可是异步会导致后面突然再执行,这样就出很多问题 2. ...

  5. Java jstl标签使用总结

    1.在jsp文件中引用 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %&g ...

  6. Windows Azure上的大数据服务: HDInsight的介绍

    这个视频介绍了目前非常流行的大数据处理框架Hadoop的Windows Azure上的实现:HDInsight,以及利用MapReduce来对大数据进行分析,利用Hive进行查询,利用客户端Power ...

  7. FocusBI:租房分析&星型模型

    微信公众号:FocusBI关注可了解更多的商业智能.数据仓库.数据库开发.爬虫知识及沪深股市数据推送.问题或建议,请关注公众号发送消息留言;如果你觉得FocusBI对你有帮助,欢迎转发朋友圈或在文章末 ...

  8. [Mysql 查询语句]——对查询结果进一步的操作

    distinct 不显示重复的查询结果 (1) 对于表中一些没有唯一性约束的字段,可能存在重复的值,这时可以使用distinct来消除那些查询结果中的重复值 select cust_id  from ...

  9. js 继承介绍

    js中继承的方式并不是明确的,这里介绍常用的几种 一.对象冒充(构造函数绑定) 原理:使用对象冒充继承基类,实质上是使用call或apply方法改变this 指针的指向 function Monkey ...

  10. google运维解密

    1.运维团队与开发团队的矛盾: 运维追求业务的稳定.开发更关注新功能的添加与版本的快速迭代.但是由于业务更新,有很大可能导致故障.从本质上来说,两部门是矛盾的. deops应该是: 1.对重复性工作有 ...