Example Elevator design Pieter van den Hombergh Fontys Hogeschool - - PowerPoint PPT Presentation

example elevator design
SMART_READER_LITE
LIVE PREVIEW

Example Elevator design Pieter van den Hombergh Fontys Hogeschool - - PowerPoint PPT Presentation

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


slide-1
SLIDE 1

Example Elevator design

Pieter van den Hombergh

Fontys Hogeschool voor Techniek en Logistiek

September 30, 2016

slide-2
SLIDE 2

Example Elevator 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

Content

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 2/26

slide-3
SLIDE 3

Example Elevator 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

State

Classification: Behavioral Intent: Allow an object to alter its behavior when its internal state changes. This can be used to make an

  • bject appear as if it changed its class.

Motivation:

In reactive systems operations (events) have a different result in different states of an object In OO design often the behavior of a class is conveniently expressed in terms of state charts. One would like to implement the behavior straightforwardly 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

slide-4
SLIDE 4

Example Elevator 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

Elevator example

Class LiftCage:

Events:

visit arriveAt doorIsclosed

LightedButton floor clear() press() Motor destination floor raise() lower() justAbove() justBelow() setDestination() LiftCage currentFloor visit(floor) arriveAt(floor) doorIsClosed() floor : int floor : int floorRequestButton theMotor Door closureTimeOut

  • pen()

close() stopClosing() theDoor

States:

Cruising: the lift cage is moving towards some 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

slide-5
SLIDE 5

Example Elevator 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

State

Participants:

Context:

defines the interface of interest to clients. maintains an instance of a ConcreteState subclass that defines the current state.

State:

defines an interface for encapsulating the behavior associated with a particular state of the context

ConcreteState:

each subclass implements behavior associated with a state of the context.

HOM/FHTenL Example Elevator design September 30, 2016 5/26

slide-6
SLIDE 6

Example Elevator 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

State

Applicability:

An object behavior depends on its state. This is often the case in reactive systems and is expressed in state charts of objects. Operations may have large multi-part conditional statements that involve the objects state. The state pattern puts each branch of the conditional in a separate subclass of the state abstraction. It is expected that the behavior of a class, in particular the number of states, may change later or is likely to change by sub-classing the 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

slide-7
SLIDE 7

Example Elevator 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

State

Consequences:

It localizes state-specific behavior. It makes transitions explicit. State objects can be shared between different contexts. ( If the state itself is state-less). It may help to implement behavior expressed in complex state charts. It requires either that the context’s state variables are part of the concrete state objects, or that the concrete states have access to the state variables of the context. To avoid breaking encapsulation such state variables 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

slide-8
SLIDE 8

Example Elevator 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

The GoF State Pattern

The state pattern uses an interface that defines the “events” that are to be accepted and uses a different class object to define the reaction in any of the defined states. 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

slide-9
SLIDE 9

Example Elevator 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

State pattern example, state diagram

HOM/FHTenL Example Elevator design September 30, 2016 9/26

slide-10
SLIDE 10

Example Elevator 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

State pattern example, class diagram for state pattern

HOM/FHTenL Example Elevator design September 30, 2016 10/26

slide-11
SLIDE 11

Example Elevator 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

Implementation remarks

Note that the state class and its subclasses do/should not have any attributes. This is typical for the state pattern. A state IS a state and does NOT have state (i.e. attributes). The operations the state can execute are defined in the context (the door in the example). A typical Java implementation (c|w)ould use inner classes for the states (with the potential problem of one big state machine file). The C++ solution would be to pass the context as a 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

slide-12
SLIDE 12

Example Elevator 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

State behavior

We assume that the desired state behavior is documented in the UML state diagram notation. Dependent on the tool that you use, it is possible to derive the necessary transition information from such a diagram. This information can be used to create an implementation. There are numerous ways to derive the implementation from the notation.

HOM/FHTenL Example Elevator design September 30, 2016 12/26

slide-13
SLIDE 13

Example Elevator 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

State behavior notation aspect

All the information that is possible and potentially available in the the UML state diagram can be notated in textual or even tabular form. The modern UML tools support a XML based format called XMI (for XML Meta-data Interchange) that may contain state model information. In all cases, a correct drawing of your state model can be used to derive a notation that can be used for automatic code generation.

HOM/FHTenL Example Elevator design September 30, 2016 13/26

slide-14
SLIDE 14

Example Elevator 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

A textual notation

Notation: (by the way: this is not XML)

≪state≫;≪event≫[≪guard≫]/{≪action list≫};≪newstate≫

where the guard is optional. For the state we have the notation:

@≪name≫;ENTRY ≪entry actions≫;EXIT

≪exit actions≫;≪initial state≫;≪history state≫

For nested state we use the notation ≪parent≫.≪child≫ Examples:

S1;e1[g1]/a1();S3.S31 @VISITFLOOR;ENTRY -; EXIT -; VISITFLOOR.OPENING

HOM/FHTenL Example Elevator design September 30, 2016 14/26

slide-15
SLIDE 15

Example Elevator 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

Nested switch statement

Traditionally the state of can be implemented using a doubly nested switch statement.

Advantages

Simple to understand. Useable in both OO and non OO languages

Disadvantages

Only maintainable in one ever growing file. Variations: Top level is state, nested are the events. Top level are events, nested are the states.

HOM/FHTenL Example Elevator design September 30, 2016 15/26

slide-16
SLIDE 16

Example Elevator 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

State − > event example in C++

void StateMachine :: handleButton(Button b){ switch (state) { case S1: switch (b.type ){ //... case UP: //... break } case S2: switch (b.type ){ // ... case DOWN: //... break } break; } }

HOM/FHTenL Example Elevator design September 30, 2016 16/26

slide-17
SLIDE 17

Example Elevator 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

Event− > state example in C++

void StateMachine :: handleButton(Button b){ switch (b.type ){ case UP: switch (state) { //... case S1: //... break } case DOWN: switch (state ){ // ... case S2: //... break } break; } }

HOM/FHTenL Example Elevator design September 30, 2016 17/26

slide-18
SLIDE 18

Example Elevator 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

Discussion of alternatives

The event − > state is advantageous in the case that different event are encoded as different method calls. Then all reaction to this event is specified in one method body.

HOM/FHTenL Example Elevator design September 30, 2016 18/26

slide-19
SLIDE 19

Example Elevator 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

Example: State model of the Elevator Cage

Note: this state model is simplified. State chart

In each of the states the response to events is different

DoorOpen entry/arrive() visit( request ) [ request.fmoor!=fmoor ]/depart() [ f==fmoor ] [ f!=fmoor ] / fmoor=f visit( request ) [ if(request==queue.fjrst())] /depart() atFloor( f ) [ else ] / depart() [ queue.isEmpty() ] doorIsClosed() Cruising Waiting Stopped

HOM/FHTenL Example Elevator design September 30, 2016 19/26

slide-20
SLIDE 20

Example Elevator 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

Implementing Complex State Behavior

Attempt 0: Keeping implicit state variables. Implicite means here: derive from inputs and state of outputs, like is motor moving

Disadvantage: relation to state diagrams obscure, hard to maintain.

Attempt 1: Introducing explicit state variables

Disadvantages: complex conditionals, still not easy to maintain

HOM/FHTenL Example Elevator design September 30, 2016 20/26

slide-21
SLIDE 21

Example Elevator 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

Explicit state variable code

Attempt 1 implementation:

c l a s s L i f t C a g e { protected i n t c u r e n t F l o o r = 0 ; protected f i n a l i n t DoorOpen = 0; protected f i n a l i n t Waiting = 1 ; protected f i n a l i n t C r u i s i n g = 2 ; // c u r r e n t s t a t e protected i n t s t a t e = Waiting ; p u b l i c L i f t C a g e () { // . . . } p u b l i c void v i s i t ( i n t f l o o r ) { i f ( s t a t e==C r u i s i n g ) { . . . } e l s e i f ( s t a t e==Waiting ) { . . . } e l s e i f ( s t a t e==DoorOpen ) { . . . } } p u b l i c void d o o r I s C l o s e d () { i f ( s t a t e==DoorOpen ) { // . . . } } p u b l i c void a t F l o o r ( i n t f l o o r ){ i f ( s t a t e==C r u i s i n g && f l o o r==c u r r e n t F l o o r ){ // . . . } } }

Attempt 1: Introducing explicit state variables

Disadvantages: complex conditionals, still not easy to maintain

HOM/FHTenL Example Elevator design September 30, 2016 21/26

slide-22
SLIDE 22

Example Elevator 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

Implementing Complex State Behavior

Attempt 2: Introducing explicit state objects (cont’d)

c l a s s L i f t C a g e { // to be accessed by s t a t e s : p u b l i c f i n a l State doorOpen = new DoorOpen ( t h i s ) ; p u b l i c f i n a l State w a i t i n g = new Waiting ( t h i s ) ; p u b l i c f i n a l State c r u i s i n g = new C r u i s i n g ( t h i s ) ; protected State s t a t e=w a i t i n g ; protected i n t c u r r e n t F l o o r =0; p u b l i c i n t g e t F l o o r () { return c u r r e n t F l o o r ( ) ; } p u b l i c void s e t F l o o r ( i n t f l o o r ) { c u r r e n t F l o o r = f l o o r ; } p u b l i c void s e t S t a t e ( State s ) { s t a t e=s ; } // to be c a l l e d by c l i e n t s : p u b l i c void v i s i t ( i n t f l o o r ) { s t a t e . v i s i t ( f l o o r ) ; } p u b l i c void d o o r I s C l o s e d () { s t a t e . d o o r I s C l o s e d ( ) ; } p u b l i c void a t F l o o r ( i n t f l o o r ){ s t a t e . a t F l o o r ( f l o o r ) ; } }

HOM/FHTenL Example Elevator design September 30, 2016 22/26

slide-23
SLIDE 23

Example Elevator 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

Implementing Complex State Behavior

Attempt 2: Introducing explicit state objects

i n t e r f a c e State { void v i s i t ( i n t f l o o r ) ; void d o o r I s C l o s e d ( ) ; void a t F l o o r ( i n t f l o o r ) ; } c l a s s C r u i s i n g implements State { protected L i f t C a g e context ; p u b l i c C r u i s i n g ( L i f t C a g e c ) { context = c ; } // . . . . p u b l i c void a t F l o o r ( i n t f l o o r ) { i f ( f l o o r== context . g e t F l o o r ( ) ) { context . s e t S t a t e ( context . doorOpen ) ; } } }

HOM/FHTenL Example Elevator design September 30, 2016 23/26

slide-24
SLIDE 24

Example Elevator 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

State

State Pattern Structure

ConcreteStateA Handle() ConcreteStateB Handle() Context Request() o State Handle() state state->Handle()

Note: Often, the state needs some resources of the context, saying that most implementations will pass the context as in handle(Context ctx) method.

HOM/FHTenL Example Elevator design September 30, 2016 24/26

slide-25
SLIDE 25

Example Elevator 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

State-less State

In attempt 2, each state constructor is bound (coupling, dependency) to one context. A better approach would be to pass the context reference with each method call. This allows state-less states, making it possible to share state objects between

  • machines. These state objects could then be created
  • nce and reused, making the object resource usage

minimal. Consequences:

Pass the context object to each state method as in void atFloor(LiftCage cage, int floor ). Implement method setNewState(State ns) in Context To top it all off, in setNewState(State ns) call state.exitState() and state.enterState(), to get exit and entry behaviour almost for free.

HOM/FHTenL Example Elevator design September 30, 2016 25/26

slide-26
SLIDE 26

Example Elevator 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

State change in context

p u b l i c State changeState ( State newState ){ State p r e v i o u s = c u r r e n t S t a t e ; p r e v i o u s . e x i t ( ) ; c u r r e n t S t a t e= newState ; c u r r e n t S t a t e . e n t e r ( ) ; return p r e v i o u s ; }

HOM/FHTenL Example Elevator design September 30, 2016 26/26