iOS 装饰模式

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

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

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

游戏手柄

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

#import 

@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 
#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 
#import 

@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);
}

你可能感兴趣的:(iOS 装饰模式)