OOPortal OOPortal


System Design  «Prev 

State design pattern

Create an attribute in the base class called 'status' or 'state' or something similar to convey its purpose.
1) Create an attribute in the base class called 'status' or 'state' or something similar to convey its purpose.

For each state in the statechart diagram, create a corresponding class definition.
2) For each state in the statechart diagram, create a corresponding class definition.

Draw a generalization relationship from each state object to a single superclass.
3) Draw a generalization relationship from each state object to a single superclass.

The data type of the state/status attribute of the base class should refer to the new superclass (this will allow the base class to refer to any of the state subclasses through this attribute). The aggregation symbol is commonly used to indicate that the state generalization is a 
'part of' the make-up of the base class.
4) The data type of the state/status attribute of the base class should refer to the new superclass (this will allow the base class to refer to any of the state subclasses through this attribute). The aggregation symbol is commonly used to indicate that the state generalization is a 'part of' the make-up of the base class.

For each event in the state transition diagram: a) Add a corresponding interface to the base object. b) Add a corresponding (identical) interface to the state superclass (this will cause every state subclass to inherit the interfaces).
5) For each event in the state transition diagram: a) Add a corresponding interface to the base object. b) Add a corresponding (identical) interface to the state superclass (this will cause every state subclass to inherit the interfaces).

To implement the base object interfaces, invoke the corresponding interface on the attribute that holds the state subclass reference.
6) To implement the base object interfaces, invoke the corresponding interface on the attribute that holds the state subclass reference.