Example Elevator design Pieter van den Hombergh Fontys Hogeschool voor Techniek en Logistiek September 30, 2016
Example Elevator Content design HOM State implementations Example: Lift Example: Lift System System Various ways to implement Traditional hand coding Various ways to state behavior Explicit State Variable implement state behavior State pattern State Objects State pattern State Behavior Notations Decoupling from Context State Behavior Notations Notation Notation State implementations Traditional hand coding Explicit State Variable State Objects Decoupling from Context HOM/FHTenL Example Elevator design September 30, 2016 2/26
Example Elevator State design HOM Classification: Behavioral Example: Lift System Intent: Allow an object to alter its behavior when its Various ways to internal state changes. This can be used to make an implement state behavior object appear as if it changed its class. State pattern Motivation: State Behavior Notations In reactive systems operations (events) have a different Notation result in different states of an object State implementations In OO design often the behavior of a class is Traditional hand coding conveniently expressed in terms of state charts. One Explicit State Variable State Objects would like to implement the behavior straightforwardly Decoupling from Context from these state charts. State charts are rather difficult to make. One does not want to throw them away when implementing classes. If one has no concurrency primitives offered in the programming language or OS and it is necessary to make a sequential program one can “fake” concurrency using the state pattern HOM/FHTenL Example Elevator design September 30, 2016 3/26
Example Elevator Elevator example design HOM LightedButton floorRequestButton Class LiftCage: floor Example: Lift System Events: clear() LiftCage press() floor : int floor : int currentFloor Various ways to visit implement state visit(floor) behavior arriveAt arriveAt(floor) doorIsClosed() theDoor State pattern doorIsclosed Door closureTimeOut State Behavior Motor Notations open() destination close() Notation floor theMotor stopClosing() State raise() lower() implementations justAbove() Traditional hand coding States: justBelow() setDestination() Explicit State Variable State Objects Cruising: the lift cage is moving towards some Decoupling from Context destination floor DoorOpen: the lift cage is at a floor with the door open Waiting: the lift cage has no pending requests and waits at a floor with the doors closed HOM/FHTenL Example Elevator design September 30, 2016 4/26
Example Elevator State design HOM Participants: Example: Lift Context: System defines the interface of interest to clients. Various ways to implement state maintains an instance of a ConcreteState subclass that behavior defines the current state. State pattern State: State Behavior Notations defines an interface for encapsulating the behavior Notation associated with a particular state of the context State implementations ConcreteState: Traditional hand coding each subclass implements behavior associated with a Explicit State Variable State Objects state of the context. Decoupling from Context HOM/FHTenL Example Elevator design September 30, 2016 5/26
Example Elevator State design HOM Applicability: Example: Lift An object behavior depends on its state. This is often System the case in reactive systems and is expressed in state Various ways to implement state charts of objects. behavior Operations may have large multi-part conditional State pattern statements that involve the objects state. The state State Behavior Notations pattern puts each branch of the conditional in a Notation separate subclass of the state abstraction. State implementations It is expected that the behavior of a class, in particular Traditional hand coding the number of states, may change later or is likely to Explicit State Variable State Objects change by sub-classing the context. Decoupling from Context Prevents coupling: No hard-coded operations in context Algorithms can be changed by changing state implementation Easier to modify class according to updated design (state chart). HOM/FHTenL Example Elevator design September 30, 2016 6/26
Example Elevator State design HOM Consequences: Example: Lift It localizes state-specific behavior. System It makes transitions explicit. Various ways to implement state State objects can be shared between different contexts. behavior ( If the state itself is state-less). State pattern It may help to implement behavior expressed in complex State Behavior Notations state charts. Notation It requires either that the context’s state variables are State implementations part of the concrete state objects, or that the concrete Traditional hand coding states have access to the state variables of the context. Explicit State Variable State Objects To avoid breaking encapsulation such state variables Decoupling from Context must be shared using C++ “friend”-like mechanisms. In Java this can be achieved in package private classes. HOM/FHTenL Example Elevator design September 30, 2016 7/26
Example Elevator The GoF State Pattern design HOM The state pattern uses an interface that defines the “events” Example: Lift that are to be accepted and uses a different System class object to define the reaction in any of the defined states. Various ways to implement state behavior State pattern State Behavior Notations Notation State implementations Traditional hand coding Explicit State Variable State Objects Decoupling from Context Advantages: The states can be defined in separate files and separately tested. On level of checking is dropped. Default reactions (e.q. logging of events) can be reused. HOM/FHTenL Example Elevator design September 30, 2016 8/26
Example Elevator State pattern example, state diagram design HOM Example: Lift System Various ways to implement state behavior State pattern State Behavior Notations Notation State implementations Traditional hand coding Explicit State Variable State Objects Decoupling from Context HOM/FHTenL Example Elevator design September 30, 2016 9/26
Example Elevator State pattern example, class diagram for state design pattern HOM Example: Lift System Various ways to implement state behavior State pattern State Behavior Notations Notation State implementations Traditional hand coding Explicit State Variable State Objects Decoupling from Context HOM/FHTenL Example Elevator design September 30, 2016 10/26
Example Elevator Implementation remarks design HOM Note that the state class and its subclasses do/should not Example: Lift have any attributes. This is typical for the state pattern. A System state IS a state and does NOT have state (i.e. attributes). Various ways to implement state behavior The operations the state can execute are defined in the State pattern context (the door in the example). A typical Java State Behavior Notations implementation (c | w)ould use inner classes for the states Notation (with the potential problem of one big state machine file). State implementations Traditional hand coding Explicit State Variable The C++ solution would be to pass the context as a State Objects Decoupling from Context parameter to the event methods, thereby preventing to have to pass the context as a attribute for the constructor. This can also be used in Java of course, although it would force you to increase the visibility of the implementation, thereby making the implementation more brittle. HOM/FHTenL Example Elevator design September 30, 2016 11/26
Example Elevator State behavior design HOM We assume that the desired state behavior is documented in Example: Lift the UML state diagram notation. System Dependent on the tool that you use, it is possible to Various ways to implement state derive the necessary transition information from such a behavior State pattern diagram. State Behavior This information can be used to create an Notations Notation implementation. State implementations There are numerous ways to derive the implementation Traditional hand coding from the notation. Explicit State Variable State Objects Decoupling from Context HOM/FHTenL Example Elevator design September 30, 2016 12/26
Example Elevator State behavior notation aspect design HOM All the information that is possible and potentially available Example: Lift in the the UML state diagram can be notated in textual or System even tabular form. Various ways to implement state behavior The modern UML tools support a XML based format called State pattern XMI (for XML Meta-data Interchange) that may contain State Behavior Notations state model information. Notation State implementations In all cases, a correct drawing of your state model can be Traditional hand coding Explicit State Variable used to derive a notation that can be used for automatic State Objects Decoupling from Context code generation. HOM/FHTenL Example Elevator design September 30, 2016 13/26
Recommend
More recommend