0

From what I've read so far, it seems that Objective C does NOT have abstract classes. I'm trying to implement a game state manager that is similar to what Apple just announced in GameKit (GKState and GKStateMachine).

My solution so far involves creating a base state class called BaseGameState and it adheres to a protocol I've created called GameState. For each state that I need for gameplay, I'm going to subclass the BaseGameState class. My BaseGameState class is just there so that I can subclass it and will not really do anything, but I want my state machine to work with one type. The class that controls which state I'm in is called GameStateMachine and it will contain an array of objects that are subclassed from BaseGameState.

To me, this seems like a horrible solution and I'm wondering if there's a standard method for doing this outside of using GameKit classes (which I cannot currently use because I need this app to target Yosemite).

02fentym
  • 1,762
  • 2
  • 16
  • 29
  • So why would you want a base class so you just have something to subclass?Just have your GameStateMachine store an array of any object conforming to your GameState protocol. (`id `). Base classes are usually not a good design decision. – Sven Jun 14 '15 at 07:18
  • Yeah, that makes sense. I had a feeling that using base classes were a bad idea, but I wasn't sure what to use instead. Thanks for the feedback. Can you write this as an answer so that I can give you credit? – 02fentym Jun 15 '15 at 04:40

1 Answers1

1

You don't need a base class, especially if it doesn't provide any implementation. This also is more flexible as it gives the user the freedom to use a different base class or extend an existing class with an implementation of your protocol.

Just have your GameStateMachine store an array of any object conforming to your GameState protocol - that is id <GameState>.

Sven
  • 22,475
  • 4
  • 52
  • 71
  • I've created my GameStateMachine as `@property NSMutableArray *states;` I have a constructor: `-(instancetype)initWithStates:(NSMutableArray*)states { if (self = [super init]) { _states = states; } return self; }` It's giving me an error though. I'm not exactly sure how to implement the solution you were talking about. Any ideas? – 02fentym Jun 19 '15 at 03:21