動(dòng)態(tài)命令:我們不需要新建各種命令類 (block實(shí)現(xiàn))
DynamicComment.h
#import "TMCommandProtocol.h"
#import "TerisMachine.h"
/動(dòng)態(tài)命令
typedef void(^DynamicBlock)(TerisMachine *);
//1.實(shí)現(xiàn)命令協(xié)議
//2.傳遞接收者
@interface DynamicComment : NSObject<TMCommandProtocol>
- (instancetype)init:(TerisMachine *)tm block:(DynamicBlock)tmBlock;
+ (id<TMCommandProtocol>)createCommand:(TerisMachine *)tm block:(DynamicBlock)tmBlock;
@end
DynamicComment.h
#import "DynamicComment.h"
@interface DynamicComment()
@property (nonatomic, strong) TerisMachine *machine;
@property (nonatomic, copy) DynamicBlock tmBlock;
@end
@implementation DynamicComment
- (instancetype)init:(TerisMachine *)tm block:(DynamicBlock)tmBlock
{
self = [super init];
if (self) {
self.machine = tm;
self.tmBlock = tmBlock;
}
return self;
}
//關(guān)于命令方式的小框架
//創(chuàng)建對象初始化的參數(shù)過于復(fù)雜我們可以內(nèi)部提供
+ (id<TMCommandProtocol>)createCommand:(TerisMachine *)tm block:(DynamicBlock)tmBlock {
return [[DynamicComment alloc]init:tm block:tmBlock];
}
- (void)execute {
self.tmBlock(self.machine);
}
@end
DynamicCommentManager.h
#import <Foundation/Foundation.h>
#import "TerisMachine.h"
NS_ASSUME_NONNULL_BEGIN
//動(dòng)態(tài)命令管理器
@interface DynamicCommentManager : NSObject
- (instancetype)init:(TerisMachine *)tm ;
- (void)toLeft;
- (void)toRight;
- (void)toTransform;
- (void)undo;
- (void)undoAll;
@end
DynamicCommentManager.m
#import "DynamicCommentManager.h"
#import "DynamicComment.h"
#import "objc/message.h"
@interface DynamicCommentManager()
@property (nonatomic, strong) TerisMachine *machine;
@property (nonatomic, strong) NSMutableArray*commands;
@end
@implementation DynamicCommentManager
- (instancetype)init:(TerisMachine *)tm {
self = [super init];
if (self) {
_machine = tm;
self.commands = [NSMutableArray array];
}
return self;
}
- (void)toLeft {
[self addCommand:NSStringFromSelector(_cmd)];
if (class_respondsToSelector([self.machine class], _cmd)) {
objc_msgSend(self.machine, _cmd);
}//判斷類中是否包含某個(gè)方法的實(shí)現(xiàn)
}
- (void)toRight {
[self addCommand:NSStringFromSelector(_cmd)];
objc_msgSend(self.machine, _cmd);
}
- (void)toTransform {
[self addCommand:NSStringFromSelector(_cmd)];
objc_msgSend(self.machine, _cmd);
}
- (void)undo {
if (self.commands.count > 0) {
NSLog(@"撤銷");
//撤銷
[[self.commands lastObject]execute];
//移除
[self.commands removeLastObject];
}
}
- (void)undoAll {
NSLog(@"撤銷所有");
//應(yīng)該倒敘刪除
for (id <TMCommandProtocol>command in self.commands) {
[command execute];
}
[self.commands removeAllObjects];
}
//根據(jù)方法名稱,動(dòng)態(tài)加載執(zhí)行對象的方法
- (void)addCommand:(NSString *)methodName {
//獲取到方法對象
SEL method = NSSelectorFromString(methodName);
//添加動(dòng)態(tài)命令
__weak DynamicCommentManager *weakSelf = self;
[self.commands addObject:[DynamicComment createCommand:self.machine block:^(TerisMachine *tm){
objc_msgSend(weakSelf.machine, method);
}]];
}
@end
main
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
NSLog(@"Hello, World!");
TerisMachine *tm = [TerisMachine new];
DynamicCommentManager *manager = [[DynamicCommentManager alloc]init:tm];
[manager toLeft];
[manager toRight];
[manager toTransform];
// [manager undo];
}
return 0;
}
來自潭州課堂