Presentation is loading. Please wait.

Presentation is loading. Please wait.

Modeling Object Lifecycles and State-Dependent Behavior ©SoftMoore ConsultingSlide 1.

Similar presentations


Presentation on theme: "Modeling Object Lifecycles and State-Dependent Behavior ©SoftMoore ConsultingSlide 1."— Presentation transcript:

1 Modeling Object Lifecycles and State-Dependent Behavior ©SoftMoore ConsultingSlide 1

2 State A state is a condition or situation during the life of an object during which it satisfies some condition, performs some activity, or waits for some event. Conceptually, a state is an abstraction of the attribute values and links of an object. Sets of values are grouped together into a state according to properties that affect gross behavior of the object. An object is said to “be in” a particular state. Example: A traffic light may be in one of three states: red, yellow, or green. ©SoftMoore ConsultingSlide 2

3 State (continued) At any instant in time, an object is usually thought of as being in exactly one state (but concurrent substates are sometimes allowed). An object usually remains in a state for a finite amount of time. Naming states –simple adjectives such as “Off” or “Active” –participial verb phrases such as “Processing” or “Closed” ©SoftMoore ConsultingSlide 3

4 State-Dependent Behavior State-dependent behavior: The object’s response to a message depends not only on the message and its parameters but also on the state of the object at the time the message is received. Most objects exhibit some state-dependent behavior. Some exhibit complex state-dependent behavior. Example: An elevator door receives a message asking it to open. The door’s response will depend on whether or not its associated elevator is moving. Example: An order receives a message to ship. Its response will depend on whether or not it is in a “hold” state pending a credit check of the customer. ©SoftMoore ConsultingSlide 4

5 State Machines A state machine specifies the sequences of states and actions that an object goes through during its lifetime in response to discrete events. A state machine models the dynamic behavior of an individual object –object lifecycle –state-dependent behavior ©SoftMoore ConsultingSlide 5 Interactions describe dynamics between several objects. State machines describe dynamics within a single object.

6 Statechart Diagrams A statechart diagram models a state machine –relates states, events, and transitions –emphasizes flow of control from state to state –notation and semantics based on work by David Harel A statechart diagram is a directed graph –nodes represent states –directed arcs represent transitions from one state to another –arcs are labeled by the event that causes the transition ©SoftMoore ConsultingSlide 6 State1State2 event

7 Notation for States A state is usually rendered as a rectangle with rounded corners May be divided into two optional compartments –name compartment (states may be anonymous) –internal transitions compartment ©SoftMoore ConsultingSlide 7 State Name entry / entry effect do / do-activity event / internal transition exit / exit effect

8 Initial and Final States Initial state –default starting place for the state machine or substate –initial pseudostate rendered as a small solid circle Final state –indicates completion of the state machine (or the enclosing state when used with nested substates) –rendered as solid circle inside an unfilled circle (bull’s eye) ©SoftMoore ConsultingSlide 8 State

9 State Versus Attribute Values Sometimes we establish a different state for each possible set of attribute values for an object. This is especially true for simple objects. Example: A lamp can have one of two states, off or on. The state of an object is often associated with some condition that must be satisfied by one or more of its attribute values. Example: Water could have an attribute called temperature that takes on many values, but we can think of water as being in one of three states – ice, liquid, or steam – based on specific ranges of its temperature. ©SoftMoore ConsultingSlide 9

10 States and Reader-Writer Operations If an object can be in a particular state, there should usually be one or more update operations (sometimes called “writers”) in the class that cause the object to be placed in that state. If an object is considered to be in a particular state, there should usually be a query operation (sometimes called a “reader”) in the class that can query the object to determine if it is in that state. ©SoftMoore ConsultingSlide 10

11 Transition An object's response to an event or stimulus is described in terms of an action performed by the object and/or a change in the state of the object. A change in state caused by an event is called a transition. When the object changes from the source state to the target state, the transition is said to “fire.” A transition is rendered as a solid directed line from the source state to the target state. ©SoftMoore ConsultingSlide 11

12 Events An event is the specification of a significant occurrence that has a location in time and space. An event occurs at a particular point in time. In the context of state machines, an event is an occurrence of a stimulus that can trigger a state transition. Conceptually, we view the occurrence of an event as being relatively fast compared to the granularity of the time scale of the overall model; event occurrence is essentially “instantaneous.” ©SoftMoore ConsultingSlide 12

13 Characteristics of Events Events may be External (between an actor and the system) or internal (between objects inside the system) Synchronous or asynchronous (analogy with phone call versus email) Intraprocess or interprocess ©SoftMoore ConsultingSlide 13

14 Example: Automobile Cruise Control System ©SoftMoore ConsultingSlide 14 OffOn CoastingCruising on button off button brake set speed resume coast button depressed coast button released brake

15 Kinds of Events Call Event –Synchronous (in general) –Invocation of an operation Signal –Asynchronous –e.g., exceptions Time Event –Asynchronous –Represents the passage of time Change Event –Asynchronous –Represents a condition becoming true ©SoftMoore ConsultingSlide 15 schedule (request) obstructionDetected after (2 seconds) when (altitude < 1000 feet)

16 Guard Conditions In addition to the event trigger, a transition may also have a guard condition that controls when the transition fires. A guard condition is rendered as a boolean expression enclosed in square brackets following the event. A guarded transition fires whenever the event trigger occurs and the condition evaluates to true. ©SoftMoore ConsultingSlide 16 item selected [balance  item cost]

17 Effects An effect is an executable behavior (action) –atomic (uninterruptable) computation. Examples –operation call –creation or destruction of an object –sending a signal to an object (uses keyword send) Effects are associated with events (performed in response to the event occurrence). A transition may include an effect that is performed when the transition fires. ©SoftMoore ConsultingSlide 17 eventName [guardCondition] / effect

18 Example: Bounded Queue ©SoftMoore ConsultingSlide 18 EmptyNonEmptyFull append append [size < capacity - 1] append [size = capacity - 1] clear remove remove [size > 1] remove [size = 1]

19 Modeling States: The Internal Transitions Compartment Entry effect: performed on entry to the state Exit effect: performed on exit from the state Do-Activity: ongoing operation performed while the object is in the state Internal transitions: performed in response to events (object does not change state) Deferred Events: (specified as “eventName / defer”) event is queued and response is postponed until the object enters a state for which it is not deferred ©SoftMoore ConsultingSlide 19

20 Triggerless Transitions A transition whose source state contains an activity is not required to have an event trigger. Such a transition is called a triggerless transition. (also called a completion transition or an automatic transition) A triggerless transition is considered to fire when the activity associated with the source state is completed. ©SoftMoore ConsultingSlide 20 State1 do / do-activity State2

21 Example: Vending Machine ©SoftMoore ConsultingSlide 21 Idle entry / set balance to 0 Collecting entry / update balance Dispensing do / dispense selected item and change money in cancel / refund balance money in item selected [balance  item cost]

22 Composite States A simple state is a state that has no substructure. A state that has substates is called a composite state. Substates may be nested to any level. A composite state may contain either sequential or concurrent substates. Substates inherit the transitions of the enclosing composite state. Any transition or action that applies to the composite state applies to all of its substates unless overridden by an equivalent transition on the substate. ©SoftMoore ConsultingSlide 22

23 Example: Automobile Cruise Control System – Version 2 ©SoftMoore ConsultingSlide 23 Off off button On set speed resume brake on button Cruising Coasting Manual Control coast button depressed coast button released

24 Modeling with State Machines A state machine is commonly associated with a class to model the lifetime and state-dependent behavior of its objects. –at most one state machine per class –may have composite states spanning more that one diagram –all objects of the class exhibit the dynamic behavior specified by the state machine Construct statechart diagrams only for classes whose objects exhibit meaningful dynamic behavior (reactive objects). Not every class requires a statechart diagram. ©SoftMoore ConsultingSlide 24


Download ppt "Modeling Object Lifecycles and State-Dependent Behavior ©SoftMoore ConsultingSlide 1."

Similar presentations


Ads by Google