STATE
BEHAVIORAL SOFTWARE DESIGN PATTERN
T.Suganya (2012/SP/031)
A.Ajantha (2012/SP/062)
K.Kopitha (2012/CSC/027)
R.Ratheeswaran (2011/CSC/009)
University Of Jaffna
What is Pattern?
 Pattern is a description of a problem and its
solution that you can apply to many
programming solution.
 Every Pattern has,
 Context
 Problem
 Solution
What is the Design Pattern ?
 Design pattern represent a solutions to problems
that arise when developing software within a
particular context.
 That are classified them in three categories,
 Creational Patterns
 Structural Patterns
 Behavioral patterns
Three Types of Patterns
 Creational Patterns (object creation)
It deal with initializing and configuring classes and objects.
 Structural Patterns (object composition)
that describe ways to assemble objects to realize new functionality.
 Behavioral patterns (communication between objects)
what will you do inside it. who will handle.
It deal with dynamic interaction among societies of classes and objects.
Introduce the pattern
We already studied
state diagram State !
Introduction – State Pattern
 An object can be in one of several states, with
different behavior in each state.
 State is one of the Behavioral pattern.
 In State pattern, we create objects which
represent various states and a context object
whose behavior varies as its state object
changes..
State Pattern
Request
Context
State = State A
Operation A
Operation B
 Intent : Allow an object to alter its behavior when its internal
state changes. The object will appear to change its class.
 Context : Maintains an instance of a Concrete State subclass
that defines the current state.
 State : Defines an interface for encapsulating the behavior
associated with a particular state of the Context.
 Concrete State : Each subclass implements a behavior
associated with a state of Context.
What is the State Design Pattern?
 An object’s behavior depends on its state and changes its behavior at
run-time.
 Operations have large, multipart conditional statements that depend
on the object's state.
 This state is usually represented by one or more enumerated constants.
 Often, several operations will contain this same conditional structure.
 The State pattern puts each branch of the conditional in a separate class.
 This lets you treat the object's state as an object in its own right that can
vary independently from other objects.
When to use the state design
pattern
 A object can be in one of several different states, i.e. its behavior is a
function of its state.
 When a object receives request form other objects, it responds
differently ( i.e.. changes its behavior ) depending on its current state.
 Such an application is likely to be characterized by large and
numerous case statements.
 But we know that conditional logic is undesirable because of
complexity, scalability or duplication.
Problem ??
State Pattern - Structure
 If we have to change the behavior of an object based on it’s state, we can
have a state variable in the Object and use if-else condition block to perform
different actions based on the state.
 State pattern is used to provide a systematic and lose-coupled way to
achieve this through Context and State implementations.
 Context is the class that has a State reference to one of the concrete
implementations of the State and forwards the request to the state object for
processing.
Illustration : Solution
State Pattern – Real Time Example
[ATM]
Think about the possible
states for the ATM
State = NoDebitcard Inserted
1.You can insert the Debit Card
2.You cannot eject the Debit Card
3.You cannot enter the pin number and withdraw the cash
1
State Pattern – Real Time Example
[ATM]
Think about the possible
states for the ATM
State = HasDebitcard [Already Inserted]
1.You cannot insert the Debit Card
2.You can eject the Debit Card
3.You can enter the pin number and withdraw the cash
2
 Context delegates state-specific request to the current concrete State
object.
 A context may pass itself as an argument to the state object handling the
request. This lets the state object access the context if necessary
 Context is the primary interface for clients. Clients can configure a context
with state objects. Once a context is configured, its clients don’t have to deal
with the state object directly
 Either context or the Concrete State subclasses can decide which state
succeeds another and under what circumstances.
Collaborations
 It localizes state-specific behavior and partitions behavior
for different states.
 It makes state transitions explicit.
 State objects can be shared.
Consequences
 The chances of error are less.
 It’s very easy to add more states for additional
behavior making it more robust.
 Easily maintainable and flexible.
The benefits of using State pattern
Demo– Real Time Example [TV Remote]
Context
State = OFF
State = ON
State

State

  • 1.
    STATE BEHAVIORAL SOFTWARE DESIGNPATTERN T.Suganya (2012/SP/031) A.Ajantha (2012/SP/062) K.Kopitha (2012/CSC/027) R.Ratheeswaran (2011/CSC/009) University Of Jaffna
  • 2.
    What is Pattern? Pattern is a description of a problem and its solution that you can apply to many programming solution.  Every Pattern has,  Context  Problem  Solution
  • 3.
    What is theDesign Pattern ?  Design pattern represent a solutions to problems that arise when developing software within a particular context.  That are classified them in three categories,  Creational Patterns  Structural Patterns  Behavioral patterns
  • 4.
    Three Types ofPatterns  Creational Patterns (object creation) It deal with initializing and configuring classes and objects.  Structural Patterns (object composition) that describe ways to assemble objects to realize new functionality.  Behavioral patterns (communication between objects) what will you do inside it. who will handle. It deal with dynamic interaction among societies of classes and objects.
  • 5.
    Introduce the pattern Wealready studied state diagram State !
  • 6.
    Introduction – StatePattern  An object can be in one of several states, with different behavior in each state.  State is one of the Behavioral pattern.  In State pattern, we create objects which represent various states and a context object whose behavior varies as its state object changes..
  • 7.
    State Pattern Request Context State =State A Operation A Operation B
  • 8.
     Intent :Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.  Context : Maintains an instance of a Concrete State subclass that defines the current state.  State : Defines an interface for encapsulating the behavior associated with a particular state of the Context.  Concrete State : Each subclass implements a behavior associated with a state of Context. What is the State Design Pattern?
  • 9.
     An object’sbehavior depends on its state and changes its behavior at run-time.  Operations have large, multipart conditional statements that depend on the object's state.  This state is usually represented by one or more enumerated constants.  Often, several operations will contain this same conditional structure.  The State pattern puts each branch of the conditional in a separate class.  This lets you treat the object's state as an object in its own right that can vary independently from other objects. When to use the state design pattern
  • 10.
     A objectcan be in one of several different states, i.e. its behavior is a function of its state.  When a object receives request form other objects, it responds differently ( i.e.. changes its behavior ) depending on its current state.  Such an application is likely to be characterized by large and numerous case statements.  But we know that conditional logic is undesirable because of complexity, scalability or duplication. Problem ??
  • 11.
    State Pattern -Structure
  • 12.
     If wehave to change the behavior of an object based on it’s state, we can have a state variable in the Object and use if-else condition block to perform different actions based on the state.  State pattern is used to provide a systematic and lose-coupled way to achieve this through Context and State implementations.  Context is the class that has a State reference to one of the concrete implementations of the State and forwards the request to the state object for processing. Illustration : Solution
  • 13.
    State Pattern –Real Time Example [ATM] Think about the possible states for the ATM State = NoDebitcard Inserted 1.You can insert the Debit Card 2.You cannot eject the Debit Card 3.You cannot enter the pin number and withdraw the cash 1
  • 14.
    State Pattern –Real Time Example [ATM] Think about the possible states for the ATM State = HasDebitcard [Already Inserted] 1.You cannot insert the Debit Card 2.You can eject the Debit Card 3.You can enter the pin number and withdraw the cash 2
  • 15.
     Context delegatesstate-specific request to the current concrete State object.  A context may pass itself as an argument to the state object handling the request. This lets the state object access the context if necessary  Context is the primary interface for clients. Clients can configure a context with state objects. Once a context is configured, its clients don’t have to deal with the state object directly  Either context or the Concrete State subclasses can decide which state succeeds another and under what circumstances. Collaborations
  • 16.
     It localizesstate-specific behavior and partitions behavior for different states.  It makes state transitions explicit.  State objects can be shared. Consequences
  • 17.
     The chancesof error are less.  It’s very easy to add more states for additional behavior making it more robust.  Easily maintainable and flexible. The benefits of using State pattern
  • 18.
    Demo– Real TimeExample [TV Remote] Context State = OFF State = ON