//  main.m
//  PolymorphicArray
//  Created by Robert Metcalfe on 11/10/13.  Copyright (c) 2013 Robert Metcalfe. All rights reserved.
#import <Foundation/Foundation.h>
#import <stdio.h>

@interface Cmd: NSObject
-(void) showIt;
-(void) execute: (Cmd *) ourcp;
@end
@implementation Cmd
-(void) showIt { printf("%s\n", "Nothing to do"); }
-(void) execute: (Cmd *) ourcp { [ourcp showIt]; }
@end

@interface CmdMessage: Cmd {
@public char * mymsg; }
-(void) showIt;
@end
@implementation CmdMessage
-(id) initWithMessage: (char *) mymessage { self = [super init];   mymsg = mymessage;  return self; }
-(void) showIt { printf("%s\n", mymsg);  }
@end

@interface CmdRepeat: Cmd {
@public Cmd * thecmd; int timesToDo; }
-(void) showIt;
@end
@implementation CmdRepeat
-(id) initWithCommand: (Cmd *) mycmd times:(int) n { self = [super init];   thecmd = mycmd;  timesToDo = n;  return self; }
-(void) showIt {  for (int i=0; i<timesToDo; i++) [thecmd showIt];   }
@end

int main(int argc, const char * argv[]) {
    CmdMessage *wakeUp = [[CmdMessage alloc] initWithMessage:"Wake up"];
    CmdMessage *getOutOfBed = [[CmdMessage alloc] initWithMessage:"Get out of bed"];
    CmdMessage *sitDownForBreakfast = [[CmdMessage alloc] initWithMessage:"Sit down for breakfast"];
    CmdMessage *eatYourGreens = [[CmdMessage alloc] initWithMessage:"Eat your greens"];
    CmdRepeat *repetition = [[CmdRepeat alloc] initWithCommand:eatYourGreens times:5];
    Cmd* thecmds[4] = {wakeUp, getOutOfBed, sitDownForBreakfast, repetition};
    for (int i=0; i<4; i++) [thecmds[i] showIt];
    return 0; }

