zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

iOS设计模式 - 装饰

2023-09-14 08:57:30 时间
// 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;

@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 10;

@end

分析

以下是装饰模式实现细节对照图

 



淘宝iOS扫一扫架构升级 - 设计模式的应用 本文在“扫一扫功能的不断迭代,基于设计模式的基本原则,逐步采用设计模式思想进行代码和架构优化”的背景下,对设计模式在扫一扫中新的应用进行了总结。
1.外观模式简介 外观模式(Facade)在开发过程中的运用频率非常高,尤其是在现阶段各种第三方SDK充斥在我们的周边,而这些SDK很大概率会使用外观模式。
模型-视图-控制器(Model-View-Controller,MVC)是Xerox PARC在20世纪80年代为编程语言Smalltalk-80发明的一种软件设计模式,至今已广泛应用于用户交互应用程序中。
建议45:设计模式是特定环境下的特定问题的解决方案 设计模式是某种特定设计的模板或指导原则。 建议46:MVC模式是一种复合或聚合模式 MVC 是一种高级别的模式,关注的是应用程序的全局架构,并根据各种对象在程序中发挥的作用对其进行分类。