美文网首页
iOS 装饰模式

iOS 装饰模式

作者: 印林泉 | 来源:发表于2017-03-06 10:45 被阅读166次
  • 装饰模式
    装饰者包含被装饰者的所有接口和引用,方法实现完全是引用调用自己的方法,在装饰者子类添加新功能。

Category不要重写被装饰对象的方法,否则改变了被装饰对象的行为,不符合装饰者模式,只可适用特殊场景。

  • 应用,适用场景
    静态库扩展功能
    不改变(原始类)、不继承、动态扩展功能。
    • 不知道原始类实现细节,只提供接口,动态扩展功能。
    • 不想有更多子类,不想通过继承的方式添加功能。
    • 动态扩展对象的功能。
    • 必须持有对象的引用,包含实例化的被装饰类。

游戏手柄

//
//  GamePad.h
//  LearnDecorator
//
//  Created by 印林泉 on 2017/3/6.
//  Copyright © 2017年 ylq. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface GamePad : NSObject

/**
 *  上下左右的操作
 */
- (void)up;
- (void)down;
- (void)left;
- (void)right;

/**
 *  选择与开始的操作
 */
- (void)select;
- (void)start;

/**
 *  按钮 A + B + X + Y
 */
- (void)commandA;
- (void)commandB;
- (void)commandX;
- (void)commandY;

@end
//
//  GamePad.m
//  LearnDecorator
//
//  Created by 印林泉 on 2017/3/6.
//  Copyright © 2017年 ylq. All rights reserved.
//

#import "GamePad.h"

@implementation GamePad

- (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");
}

- (void)commandX {
    
    NSLog(@"commandX");
}

- (void)commandY {
    
    NSLog(@"commandY");
}

@end

装饰者基类

//
//  GamePadDecorator.h
//  LearnDecorator
//
//  Created by 印林泉 on 2017/3/6.
//  Copyright © 2017年 ylq. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "GamePad.h"

@interface GamePadDecorator : NSObject

/**
 *  上下左右的操作
 */
- (void)up;
- (void)down;
- (void)left;
- (void)right;

/**
 *  选择与开始的操作
 */
- (void)select;
- (void)start;

/**
 *  按钮 A + B + X + Y
 */
- (void)commandA;
- (void)commandB;
- (void)commandX;
- (void)commandY;

@end
//
//  GamePadDecorator.m
//  LearnDecorator
//
//  Created by 印林泉 on 2017/3/6.
//  Copyright © 2017年 ylq. All rights reserved.
//

#import "GamePadDecorator.h"

@interface GamePadDecorator ()

@property (nonatomic, strong) GamePad *gamePad;

@end

@implementation GamePadDecorator

- (instancetype)init {
    self = [super init];
    if (self) {
        self.gamePad = [[GamePad alloc] init];
    }
    return self;
}

- (void)up {
    [self.gamePad up];
}

- (void)down {
    [self.gamePad down];
}

- (void)left {
    [self.gamePad left];
}

- (void)right {
    [self.gamePad right];
}

- (void)select {
    [self.gamePad select];
}

- (void)start {
    [self.gamePad start];
}

- (void)commandA {
    [self.gamePad commandA];
}

- (void)commandB {
    [self.gamePad commandB];
}

- (void)commandX {
    [self.gamePad commandX];
}

- (void)commandY {
    [self.gamePad commandY];
}

@end

作弊装饰者(方法扩展)

//
//  CheatGamePadDecorator.h
//  LearnDecorator
//
//  Created by 印林泉 on 2017/3/6.
//  Copyright © 2017年 ylq. All rights reserved.
//

#import "GamePadDecorator.h"

@interface CheatGamePadDecorator : GamePadDecorator

- (void)cheat;

@end
//
//  CheatGamePadDecorator.m
//  LearnDecorator
//
//  Created by 印林泉 on 2017/3/6.
//  Copyright © 2017年 ylq. All rights reserved.
//

#import "CheatGamePadDecorator.h"

@implementation CheatGamePadDecorator

- (void)cheat {
    [self up];
    [self down];
    [self up];
    [self down];
    [self left];
    [self right];
    [self left];
    [self right];
    [self commandA];
    [self commandB];
    [self commandA];
    [self commandB];
}

@end

游戏币装饰者(属性扩展)

//
//  CoinGamePadDecorator.h
//  LearnDecorator
//
//  Created by 印林泉 on 2017/3/6.
//  Copyright © 2017年 ylq. All rights reserved.
//

#import "GamePadDecorator.h"

@interface CoinGamePadDecorator : GamePadDecorator

/**
 游戏币
 */
@property (nonatomic) NSInteger coin;

@end
//
//  CoinGamePadDecorator.m
//  LearnDecorator
//
//  Created by 印林泉 on 2017/3/6.
//  Copyright © 2017年 ylq. All rights reserved.
//

#import "CoinGamePadDecorator.h"

@implementation CoinGamePadDecorator

@end

使用装饰者

- (void)decorator {
    GamePad *gamePad = [[GamePad alloc] init];
    [gamePad up];
    
    GamePadDecorator *gamePadDecorator = [[GamePadDecorator alloc] init];
    [gamePadDecorator up];
    
    CheatGamePadDecorator *cheatGamePadDecorator = [[CheatGamePadDecorator alloc] init];
    [cheatGamePadDecorator cheat];
}

作弊Category(方法扩展)

//
//  GamePad+Cheat.h
//  LearnDecorator
//
//  Created by 印林泉 on 2017/3/6.
//  Copyright © 2017年 ylq. All rights reserved.
//

#import "GamePad.h"

@interface GamePad (Cheat)

- (void)cheat;

@end
//
//  GamePad+Cheat.m
//  LearnDecorator
//
//  Created by 印林泉 on 2017/3/6.
//  Copyright © 2017年 ylq. All rights reserved.
//

#import "GamePad+Cheat.h"

@implementation GamePad (Cheat)

- (void)cheat {
    [self up];
    [self down];
    [self up];
    [self down];
    [self left];
    [self right];
    [self left];
    [self right];
    [self commandA];
    [self commandB];
    [self commandA];
    [self commandB];
}

@end

游戏币Category(属性扩展)

//
//  GamePad+Coin.h
//  LearnDecorator
//
//  Created by 印林泉 on 2017/3/6.
//  Copyright © 2017年 ylq. All rights reserved.
//

#import "GamePad.h"

@interface GamePad (Coin)

/**
 游戏币
 */
@property (nonatomic) NSInteger coin;

@end
//
//  GamePad+Coin.m
//  LearnDecorator
//
//  Created by 印林泉 on 2017/3/6.
//  Copyright © 2017年 ylq. All rights reserved.
//

#import "GamePad+Coin.h"
#import <objc/runtime.h>
#import <Foundation/Foundation.h>

@implementation GamePad (Coin)

static const NSString *_coinStr = @"_coinStr";

#pragma mark - 动态给属性添加关联

- (void)setCoin:(NSInteger)coin {
    objc_setAssociatedObject(self, (__bridge const void *)_coinStr, @(coin), OBJC_ASSOCIATION_RETAIN);
}

- (NSInteger)coin {
    NSNumber *number = objc_getAssociatedObject(self, (__bridge const void *)_coinStr);
    return number.integerValue;
}

@end

Category使用

- (void)category {
    GamePad *gamePad = [[GamePad alloc] init];
    [gamePad cheat];
    
    gamePad.coin = 10;
    NSLog(@"coin %ld", (long)gamePad.coin);
}

相关文章

  • 03-设计模式(包括KVO/KVC)

    iOS开发常用设计模式? 详细blog链接 MVC模式 MVVM模式 代理模式 单例模式 工厂模式 装饰者模式 观...

  • iOS 装饰模式

    装饰模式装饰者包含被装饰者的所有接口和引用,方法实现完全是引用调用自己的方法,在装饰者子类添加新功能。 Categ...

  • 设计模式-装饰器

    这里介绍一下设计模式中的装饰器模式在iOS中的实现。 一、用来做什么的?装饰器模式[Decorator Patte...

  • iOS设计模式:装饰

    视频资源-极客学院 ps:感觉打的一手好广告啊,因为自己不太爱看视频,但这类又必须看才能明白。粘贴源代码是为了以后...

  • iOS设计模式五(访问者,装饰,责任链)

    承接上文iOS设计模式四(组合,迭代器)本文为行为扩展--获取源码 目录1 访问者模式2 装饰模式3 责任链模式 ...

  • iOS设计模式-装饰者模式

    一、定义 装饰者模式动态的将责任附加到对象上。若要扩展功能,装饰者提供了比继承更有弹性的替代方案。 装饰者模式采用...

  • iOS设计模式-装饰者模式

    问题: 某煎饼店现有煎饼、手抓饼和烧饼三种商品出售,每种商品都可以另外加鸡蛋或者香肠,现要开发一套商品结算系统,那...

  • iOS设计模式-装饰模式- Swift

    装饰模式 1、装饰模式-定义动态地给一个对象添加一些额外的职责。 2、装饰模式-场景需要透明且动态地扩展类的功能时...

  • 『ios』设计模式——装饰器模式

    什么是装饰器模式?动态地为一个对象添加一些额外的职责,若要扩展一个对象的功能,装饰者提供了比继承更有弹性的替代方案...

  • iOS设计模式-装饰器模式

    https://www.jianshu.com/p/1528a4082fc1[https://www.jiansh...

网友评论

      本文标题:iOS 装饰模式

      本文链接:https://www.haomeiwen.com/subject/lqyygttx.html