设计模式调整
- 引入命令队列:
- 可以创建一个命令队列类(如
CommandQueue
),将命令对象按照一定规则(如优先级)加入队列。在CommandQueue
类中,可以定义一个数组或其他合适的数据结构来存储命令对象。
- 例如:
@interface CommandQueue : NSObject
@property (nonatomic, strong) NSMutableArray<id<CommandProtocol>> *commandQueue;
- (void)addCommand:(id<CommandProtocol>)command;
- (void)executeNextCommand;
@end
@implementation CommandQueue
- (instancetype)init {
self = [super init];
if (self) {
_commandQueue = [NSMutableArray array];
}
return self;
}
- (void)addCommand:(id<CommandProtocol>)command {
[self.commandQueue addObject:command];
}
- (void)executeNextCommand {
if (self.commandQueue.count > 0) {
id<CommandProtocol> command = self.commandQueue.firstObject;
[command execute];
[self.commandQueue removeObjectAtIndex:0];
}
}
@end
- 命令聚合:
- 对于一些经常一起执行的命令,可以将它们组合成一个复合命令。例如,在游戏开发中,角色的移动、攻击等操作可能需要多个命令协同完成,可以将这些命令封装到一个
CompositeCommand
类中。
- 定义
CompositeCommand
遵循CommandProtocol
协议,并在其中管理子命令数组。
@protocol CommandProtocol <NSObject>
- (void)execute;
@end
@interface CompositeCommand : NSObject <CommandProtocol>
@property (nonatomic, strong) NSMutableArray<id<CommandProtocol>> *subCommands;
- (void)addSubCommand:(id<CommandProtocol>)command;
@end
@implementation CompositeCommand
- (instancetype)init {
self = [super init];
if (self) {
_subCommands = [NSMutableArray array];
}
return self;
}
- (void)addSubCommand:(id<CommandProtocol>)command {
[self.subCommands addObject:command];
}
- (void)execute {
for (id<CommandProtocol> command in self.subCommands) {
[command execute];
}
}
@end
代码结构优化
- 模块化命令:
- 将不同类型的命令分别放到不同的文件和类中,例如将用户界面相关的命令放在
UICommands
文件夹下,业务逻辑相关的命令放在BusinessLogicCommands
文件夹下。
- 这样可以使代码结构更加清晰,易于查找和维护。比如,对于用户点击按钮的命令,可以定义
ButtonClickCommand
类。
@interface ButtonClickCommand : NSObject <CommandProtocol>
@property (nonatomic, weak) UIButton *button;
- (instancetype)initWithButton:(UIButton *)button;
@end
@implementation ButtonClickCommand
- (instancetype)initWithButton:(UIButton *)button {
self = [super init];
if (self) {
_button = button;
}
return self;
}
- (void)execute {
// 处理按钮点击后的逻辑
NSLog(@"Button %@ clicked", self.button.titleLabel.text);
}
@end
- 使用协议和分类:
- 通过协议来定义命令的基本行为,如
CommandProtocol
。然后可以使用分类为不同类型的对象添加执行命令的能力。
- 例如,为
UIViewController
添加一个分类,使其能够执行特定类型的命令。
@interface UIViewController (CommandExecution)
- (void)executeCommand:(id<CommandProtocol>)command;
@end
@implementation UIViewController (CommandExecution)
- (void)executeCommand:(id<CommandProtocol>)command {
[command execute];
}
@end
多线程处理
- 异步执行命令:
- 使用
Grand Central Dispatch
(GCD)来异步执行命令,避免阻塞主线程。对于一些耗时较长的命令,如网络请求命令,可以将其放到后台队列执行。
- 例如:
@interface NetworkRequestCommand : NSObject <CommandProtocol>
- (void)execute;
@end
@implementation NetworkRequestCommand
- (void)execute {
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
// 网络请求逻辑
NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:@"http://example.com"] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
// 处理请求结果
if (!error && data) {
NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"Network request result: %@", result);
}
}];
[task resume];
});
}
@end
- 线程安全处理:
- 如果多个命令可能会同时访问和修改共享资源,需要使用锁机制(如
NSLock
或dispatch_semaphore
)来保证线程安全。
- 例如,在一个共享数据的读写命令中:
@interface SharedDataWriteCommand : NSObject <CommandProtocol>
@property (nonatomic, strong) id sharedData;
@property (nonatomic, strong) NSLock *lock;
- (instancetype)initWithSharedData:(id)sharedData lock:(NSLock *)lock;
@end
@implementation SharedDataWriteCommand
- (instancetype)initWithSharedData:(id)sharedData lock:(NSLock *)lock {
self = [super init];
if (self) {
_sharedData = sharedData;
_lock = lock;
}
return self;
}
- (void)execute {
[self.lock lock];
// 写入共享数据逻辑
self.sharedData = @"new value";
[self.lock unlock];
}
@end