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. 控制反转(IOC) 和依赖注入(DI) 的理解

    1.      IOC(控制反转) inverseof control是spring容器的内核,AOP.声明事务等功能在此基础上开花结果. 2.      通过实例理解IOC概念: 实例:<墨攻 ...

  2. MyBatis异常总结

    1 Invalid bound statement 1 org.apache.ibatis.binding.BindingException: Invalid bound statement (not ...

  3. WPF的System.Windows.Threading.DispatcherTimer的使用(每隔一定的时间重复做某事)

    这里使用了一个进度条来展示, 前段代码: <Window x:Class="TimerTest.MainWindow" xmlns="http://schemas. ...

  4. [笔记] Python基础---列表

    1.定义列表 列表由数据构成的有限序列,即按照一定的线性顺序排列,排列而成的数据项的集合. 2.创建列表 使用‘[ ]’括起来就已经创建了一个列表,例: my_first_list = [] #空列表 ...

  5. WebLogic 数据源密码加密

    先启动域:

  6. SQL性能调优

    部分转自:http://www.cnblogs.com/luckybird/archive/2012/06/11/2544753.html 及http://www.cnblogs.com/kissdo ...

  7. vue 实战问题-watch 数组或者对象

    1.普通的watch data() { return { frontPoints: 0 } }, watch: { frontPoints(newValue, oldValue) { console. ...

  8. Linux du查询文件大小

    #查询磁盘当前容量信息 $df -h   #查询当前目录下所有文件的大小 $du -m .   #两种方式查询 仅当前目录下的子文件(文件夹)大小 $du -sh /cloud/*   $du -h ...

  9. [转]C#中Timer使用及解决重入问题

    本文转自:http://www.cnblogs.com/hdkn235/archive/2014/12/27/4187925.html ★前言 打开久违的Live Writer,又已经好久没写博客了, ...

  10. Eclipse 反编译之 JadClipse

    一:下载对应的 net.sf.jadclipse_x.x.x.jar ,把该jar包放入到Eclipse中的 plugins 目录下,下载地址:https://sourceforge.net/proj ...