tags:

views:

46

answers:

2

I am modeling a small State diagram. Each "state class needs to have its transition class. How do I ensure the developers make a transition class for each state class? The state and the transition both are subclassed from their corresponding Abstract classes.

EDIT: I was working on the Head first Design Patterns and I tried to make my own states. i got stuck here. Not a homework.

A: 

In a generic FSM a state node can have any number of incoming and outgoing transition arcs (including zero incoming -- start state, or zero outgoing -- end state, but not both). The problem is not as simple as one transition per state. The state and transition objects by themselves cannot validate the entire FSM. That has to happen from "outside" the states and transitions.

Jim Garrison
A: 

If your State object requires at least one Transition, require a Transition object or a Collection of Transitions in the constructor.

class Transition { ... }
class State {
  Collection<Transition> transitions;
  ...
  State(Collection<Transition> t){
    //make sure the collection is non empty
    ...
    //save the transition
    transitions = t;
  }
  ...
}
Matthew Sowders