阅读下列说明和Objective C代码,将应填入 (n) 处的字句写在答题纸的对应栏内。
备注:可以将以下代码翻译成自己擅长的语言之后,再回答相应的问题。
【说明】某灯具厂商欲生产一个灯具遥控器,该遥控器具有7个可编程的插槽,每个插槽都有开关灯具的开关,现采用Command(命令)模式实现该遥控器的软件部分。Command模式的类图如图1-1所示。
@interface Light : NSObject
- (void)on;
- (void)off;
@end
(1)
- (void)execute;
@end
@interface LightOnCommand : NSObject <Command>
@property (strong, nonatomic) Light *light;
- (id)initWithLight:(Light *)light;
@end
@implementation LightOnCommand
- (id)initWithLight:(Light *)light{
if (self = [super init]) {
self.light = light;
}
return self;
}
- (void)execute{
(2)
}
@end
@interface LightOffCommand : NSObject <Command>
@property (strong, nonatomic) Light *light;
- (id)initWithLight:(Light *)light;
@end
@implementation LightOffCommand
- (id)initWithLight:(Light *)light{
if (self = [super init]) {
self.light = light;
}
return self;
}
- (void)execute{
(3)
}
@end
@interface RemoteControl : NSObject
@property (strong, nonatomic) NSMutableArray* onCommands;
@property (strong, nonatomic) NSMutableArray* offCommands;
- (void)setCommandWithSlot:(int)slot onCommand:(id<Command>)onCommand andOffCommand:(id<Command>)offCommand;
- (void)onButtonWasPushedAtSlot:(int)slot;
- (void)offButtonWasPushedAtSlot:(int)slot;
@end
@implementation RemoteControl
- (id)init{
if (self = [super init]) {
self.onCommands = [NSMutableArray array];
self.offCommands = [NSMutableArray array];
}
return self;
}
- (void)setCommandWithSlot:(int)slot onCommand:(id<Command>)onCommand andOffCommand:(id<Command>)offCommand{
(4) = onCommand;
(5) = offCommand;
}
- (void)onButtonWasPushedAtSlot:(int)slot{
(6)
}
- (void)offButtonWasPushedAtSlot:(int)slot{
(7)
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
RemoteControl *remoteControl = [[RemoteControl alloc] init];
Light *livingRoomLight = [[Light alloc] init];
Light *kitchenLight = [[Light alloc] init];
LightOnCommand *livingRoomLightOn = [[LightOnCommand alloc] initWithLight:livingRoomLight];
LightOffCommand *livingRoomLightOff = [[LightOffCommand alloc] initWithLight:livingRoomLight];
LightOnCommand *kitchenRoomLightOn = [[LightOnCommand alloc] initWithLight:kitchenLight];
LightOffCommand *kitchenRoomLightOff = [[LightOffCommand alloc] initWithLight:kitchenLight];
[remoteControl setCommandWithSlot:0 onCommand:livingRoomLightOn andOffCommand:livingRoomLightOff];
[remoteControl setCommandWithSlot:1 onCommand:kitchenRoomLightOn andOffCommand:kitchenRoomLightOff];
[remoteControl onButtonWasPushedAtSlot:0];
[remoteControl offButtonWasPushedAtSlot:0];
[remoteControl onButtonWasPushedAtSlot:1];
[remoteControl offButtonWasPushedAtSlot:1];
}
return 0;
}
5题 | 被引用1次