iOS设计模式:装饰

2017-03-01  本文已影响29人  younger_times

视频资源-极客学院

ps:感觉打的一手好广告啊,因为自己不太爱看视频,但这类又必须看才能明白。粘贴源代码是为了以后查阅方便,也注释了自己的理解。

GamePad.h(Component)

原生产厂家生产的游戏手柄

#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

#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 (Decorator)

装饰者,几乎和GamePad一样的操作


/**

 *  上下左右的操作

 */

- (void)up;

- (void)down;

- (void)left;

- (void)right;

/**

 *  选择与开始的操作

 */

- (void)select;

- (void)start;

/**

 *  按钮 A + B + X + Y

 */

- (void)commandA;

- (void)commandB;

- (void)commandX;

- (void)commandY;

#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]; #初始化时,实例化GamePad

    }

    

    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];

}

GamePadDecorator.h(ConcreteDecoratorA)

装饰者的拓展,可以添加新方法


#import "GamePadDecorator.h"

@interface CheatGamePadDecorator : GamePadDecorator

- (void)cheat;

@end

#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];

}

iOS的category进行扩展

GamePad+Cheat.h

#import "GamePad.h"

@interface GamePad (Cheat)

- (void)cheat;

@end
#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
上一篇下一篇

猜你喜欢

热点阅读