动态命令:我们不需要新建各种命令类 (block实现)
DynamicComment.h
#import "TMCommandProtocol.h"
#import "TerisMachine.h"
/动态命令
typedef void(^DynamicBlock)(TerisMachine *);
//1.实现命令协议
//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;
}
//关于命令方式的小框架
//创建对象初始化的参数过于复杂我们可以内部提供
+ (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
//动态命令管理器
@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);
}//判断类中是否包含某个方法的实现
}
- (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(@"撤销所有");
//应该倒叙删除
for (id <TMCommandProtocol>command in self.commands) {
[command execute];
}
[self.commands removeAllObjects];
}
//根据方法名称,动态加载执行对象的方法
- (void)addCommand:(NSString *)methodName {
//获取到方法对象
SEL method = NSSelectorFromString(methodName);
//添加动态命令
__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;
}
来自潭州课堂
网友评论