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. 《LeetBook》leetcode题解(7): Reverse Integer[E]——处理溢出的技巧

    我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 书的地址:https://hk029.gitbooks.io/leetboo ...

  2. git提交代码到远程仓库

    1.仓库初始化 git init 2.连接仓库 git remote add origin 仓库地址 3.查看状态 git status 4.将文件添加到暂存区 git add 状态里的新文件 5.将 ...

  3. Redis之hiredis API (String)

    String // // Created by zhangrongxiang on 2018/3/7 13:48 // File string2 // #include <hiredis/hir ...

  4. 前端自动化Gulp工具常用插件

    npm init命令初始化当前文件夹后,在当前文件夹新建gulpfile.js文件.当前目录下的所有操作流都在gulpfile.js文件中定义. gulp自动化 gulp-uglify (JS压缩) ...

  5. 第二章:第2章PHP基础语法

    一. 基本的PHP语法如下: 1.PHP脚本可以放在文档中的任何位置,PHP脚本以<?php开始,以?>结束 <?php   //PHP代码 ?> 2.php文件的默认文件扩展 ...

  6. Ruby(1):入门

    安装: 一般来说linux会自动装ruby,可以通过: ruby -v 来查看ruby版本 直接使用ruby命令的话,是用来执行ruby文件的.如果要打开交互式ruby解释器.只需要在控制台输入:ir ...

  7. vs2015 点击cshtml 后提示 "无效指针" 的解决办法

    1. 关闭vs 2. 删除 %LocalAppData%\Microsoft\VisualStudio\14.0\ComponentModelCache 3. 打开vs OK 解决

  8. jQuery事件篇---事件对象

    内容提纲: 1.事件对象 2.冒泡和默认行为 发文不易,转载请注明出处! JavaScript 在事件处理函数中默认传递了 event 对象,也就是事件对象.但由于浏览器的兼容性,开发者总是会做兼容方 ...

  9. 基于标注的AOP面向切面编程

    1.什么是AOP Aspect  Orientied   Programming的简称,即 面向(方面)切面编程 ,不改变一个组件源代码的情况下 可以对组件功能进行增强. 例如:servlet中的过滤 ...

  10. 【C#】权限修饰符

    这个看了蛮多遍的,但是由于有一些一直不用,老是忘记,记录一下:) private  成员只能由同一个类(class)类型中的其他成员访问. family  成员可由派生类访问,不管那些类型是否在用一个 ...