0

I need to implements the state machine in objective-c like this java code:

public enum State {

    INITIAL {
        @Override
        State doSomething(String aParameter) {
            System.out.println("Doing Something in INITIAL state and jumping to NEXT_STEP, argument = " + aParameter);
            return NEXT_STEP;
        }
    },
    NEXT_STEP {
        @Override
        State doSomething(String aParameter) {
            System.out.println("Doing Something in NEXT_STEP and jumping into FINAL, argument = " + aParameter);
            return FINAL;
        }
    },
    FINAL {
        @Override
        State doSomething(String aParameter) {
            System.out.println("I am in FINAL state, argument = " + aParameter);
            return this;
        }
    };

    abstract State doSomething(String aParameter);
}

the reference link is: http://www.mirkosertic.de/doku.php/javastuff/enumstatemachine

someone can give me some suggestions?

  • 1
    This does not look like ObjectiveC. – Matthias May 22 '14 at 14:13
  • yes, you're right, my request is not exactly clear; this code is java but I want to get the same result in objetive c – Massimiliano D'Amico May 22 '14 at 14:16
  • Check [CocoaPods](http://cocoapods.org/?q=statemachine), there are several state machines available. – zaph May 22 '14 at 14:17
  • You can't do this with `C` enums. See this SO post for the difference between `C++` and `Java` enums (applies to `C` too):http://stackoverflow.com/q/2080681/558933 – Robotic Cat May 22 '14 at 14:17
  • Also, see the following SO post on how to make a basic finite state machine in `Objective-C`:http://stackoverflow.com/questions/2412720/how-to-make-a-basic-finite-state-machine-in-objective-c – Robotic Cat May 22 '14 at 14:19
  • 4
    There are a dozen different ways to do that, none of them involving enums, except perhaps to enumerate the possible states used in a table. Using good ol' `switch` would be the most obvious choice, or multiple subclasses of a common class. On any of several variations on function pointers -- straight C or Objective-C versions. – Hot Licks May 22 '14 at 15:13

1 Answers1

1

Thanks to the information received, and according to the need that I had..

I hope to have found a best solution, and I share it:

file StateHandler.h :

enum State {
    INITIAL,
    NEXT_STEP,
    FINAL
};

@interface StateHandler

+(enum State)doSomething:(NSString *)aParameter withState:(enum State)state;

@end

file StateHandler.m :

#import "StateHandler.h"

@protocol StateProtocol <NSObject>
@required +(enum State)doSomething:(NSString *)aParameter;
@end

@interface StateInitial : NSObject <StateProtocol>
@end

@interface StateNextStep : NSObject <StateProtocol>
@end

@interface StateFinal : NSObject <StateProtocol>
@end

@implementation StateHandler

+(enum State)doSomething:(NSString *)aParameter withState:(enum State)state {
    switch(state) {
        case INITIAL:
            return [StateInitial doSomething:aParameter];

        case NEXT_STEP:
            return [StateNextStep doSomething:aParameter];

        case FINAL:
            return [StateFinal doSomething:aParameter];
    }
}
@end

@implementation StateInitial
+(enum State)doSomething:(NSString *)aParameter {
    NSLog(@"Doing Something in INITIAL state and jumping to NEXT_STEP, argument = %@", aParameter);
    return NEXT_STEP;
}
@end

@implementation StateNextStep
+(enum State)doSomething:(NSString *)aParameter {
    NSLog(@"Doing Something in NEXT_STEP and jumping into FINAL, argument = %@", aParameter);
    return FINAL;
}
@end

@implementation StateFinal
+(enum State)doSomething:(NSString *)aParameter {
    NSLog(@"I am in FINAL state, argument = %@", aParameter);
    return FINAL;
}
@end
  • Wow... so much complexity ! Do you really need different Classes for different steps ? Also, you use return values to point to next enum, but you never use this return value in your code... You should definitely look to `NSOperation` – Vinzzz May 22 '14 at 22:27
  • Another possibility would be to create a singleton, initialized with the state, which retains its state in an attribute inside. For each method call "doSomething" controls with a switch status and dispatches to a method inside .. what do you think @Vinzzz ? any suggestion is welcome – Massimiliano D'Amico May 23 '14 at 07:38
  • 1
    I think, like another comment said, there are dozens of way to do that. If you're looking for inspiration, another example of state machine from Apple in iOS is `UIGestureRecognizer`, though I'm not saying this particular design would be the one to copy... My only humble advice : don't over-design things, Keep It Simple ;) – Vinzzz May 23 '14 at 08:26
  • This is not very smart way how to implement FSM. Missing state transitions. Basically this is if else conditions written in objective way. – mauron85 Feb 23 '18 at 12:33