@
interface
EleControl(){
Command *undoCommand;
}
@property (strong,nonatomic) NSMutableArray *onCommands;
@property (strong,nonatomic) NSMutableArray *offCommands;
@end
@implementation EleControl
-(instancetype)initWithCommandCount:(NSInteger)count{
self=[super init];
if
(self) {
for
(NSInteger i=0; i<count; i++) {
[self.onCommands addObject:[NSNull
null
]];
[self.offCommands addObject:[NSNull
null
]];
}
}
return
self;
}
-(
void
)setupCommand:(NSInteger)index onCommand:(Command *)onCommand offCommand:(Command *)offCommand{
self.onCommands[index]=onCommand;
self.offCommands[index]=offCommand;
}
-(
void
)controlPressedOn:(NSInteger)index{
Command *cmd=[self.onCommands objectAtIndex:index];
[cmd excute];
undoCommand=cmd;
}
-(
void
)controlPressedOff:(NSInteger)index{
Command *cmd=[self.offCommands objectAtIndex:index];
[cmd excute];
undoCommand=cmd;
}
-(
void
)controlPressedUndo{
[undoCommand undo];
}
#pragma mark getter and setter
-(NSMutableArray *)onCommands{
if
(!_onCommands) {
_onCommands=[[NSMutableArray alloc]init];
}
return
_onCommands;
}
-(NSMutableArray *)offCommands{
if
(!_offCommands) {
_offCommands=[[NSMutableArray alloc]init];
}
return
_offCommands;
}
@end