SlideShare a Scribd company logo
1
DESIGN PATTERNS :
State Pattern
By
RAVI P. PATKI
Associate Professor (IT)
Hope Foundation’s
International Institute of Information Technology
2
Agenda
 Introduction
 Problem in Software Design /code
 Definition of State Pattern
 Solution to Problem in terms of
Strategy Pattern
 Advantages
 Implementation of State Pattern
 Applications
Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi
Infotech Park, Hinjawadi, Pune - 411 057
Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
3
Introduction
State pattern is one of the behavioral design
pattern.
State design pattern is used when an Object change
it’s behavior based on it’s internal state.
The state pattern is a behavioral software design
pattern that implements a state machine in an
object-oriented way.
With the state pattern, a state machine is
implemented by implementing each individual state
as a derived class of the state pattern interface, and
implementing state transitions by invoking methods
defined by the pattern's super class.
Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi
Infotech Park, Hinjawadi, Pune - 411 057
Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
4
 This pattern is used in computer programming to
encapsulate varying behavior for the same
object based on its internal state.
 This can be a cleaner way for an object to
change its behavior at runtime without resorting
to large monolithic /huge conditional
statements and thus improve maintainability.
Introduction
Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi
Infotech Park, Hinjawadi, Pune - 411 057
Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
5
Example
 The State pattern allows an object to change its behavior when its internal
state changes. This pattern can be observed in a vending machine.
 Vending machines have states based on the inventory, amount of currency
deposited, the ability to make change, the item selected, etc.
 When currency is deposited and a selection is made, a vending machine will
either
deliver a product and no change,
deliver a product and change,
deliver no product due to insufficient currency on deposit, or
deliver no product due to inventory depletion.
Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi
Infotech Park, Hinjawadi, Pune - 411 057
Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
6
Problem in Software Design
 Tired of conditionals?
 When writing code, our classes often go through a series of
transformations.
 What starts out as a simple class will grow as behavior is added.
 if you didn’t take the necessary precautions, your code will become
difficult to understand and maintain.
 Too often, the state of an object is kept by creating multiple Boolean
attributes and deciding how to behave based on the values.
 This can become cumbersome and difficult to maintain when the
complexity of your class starts to increase.
 This is a common problem on most projects.
Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi
Infotech Park, Hinjawadi, Pune - 411 057
Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
7
Suppose we want to implement a TV Remote object with a simple button to
perform action, if the State is ON, it will turn on the TV and if state is OFF, it will
turn off the TV. We can implement it using if-else condition like below;
Problem in Software Design
Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi
Infotech Park, Hinjawadi, Pune - 411 057
Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
8
Problem in Software Design
What are the problems with above design?
Notice that
 client code should know the specific values to use for setting the state of
remote,
 further more if number of states increase then the tight coupling between
implementation and the client code will be very hard to maintain and extend.
 How can we avoid this?
 Design / Code will become more cleaner by using Enums and Switches or
multiple if then else (Similar to our SavingAccount object in Assignment no.3)
 Here in this solutions Instead of a bunch of flags, we will just have one state_
field. We also flip the order of our branching.
 But again code become monolithic or as single block.
 This is a common problem on most projects, and it is wise to model it with a
Finite State Machine.
 In fact, there is a design pattern called State that address this very well, so
you can find hundreds of gems implementing this pattern.
Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi
Infotech Park, Hinjawadi, Pune - 411 057
Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
9
Definition : State Pattern
 The State pattern is known as a behavioural pattern - it's
used to manage algorithms, relationships and
responsibilities between objects.
 In State pattern a class behavior changes based on its
state. This type of design pattern comes under behavior
pattern.
 In State pattern, we create objects which represent
various states and a context object whose behavior varies
as its state object changes.
 The definition of State provided in the original Gang of
Four book on Design Patterns states:
“Allows an object to alter its behaviour when its
internal state changes. The object will appear to
change its class.”
Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi
Infotech Park, Hinjawadi, Pune - 411 057
Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
10
Definition : State Pattern
Let's take a look at the diagram definition
The Context can have a number of
internal States, whenever the
request() method is called on the
Context, the message is delegated
to the State to handle.
The State interface defines a
common interface for all concrete
states, encapsulating all behaviour
associated with a particular state.
The ConcreteState implements it's
own implementation for the request.
When a Context changes state, what
really happens is that we have a
different ConcreteState associated
with it.
Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi
Infotech Park, Hinjawadi, Pune - 411 057
Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
11
Definition : State Pattern
Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi
Infotech Park, Hinjawadi, Pune - 411 057
Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
12
Solution to Problem in Terms of State
State Interface
First of all we will create State interface that will define
the method that should be implemented by different
concrete states and context class.
Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi
Infotech Park, Hinjawadi, Pune - 411 057
Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
13
Solution to Problem in Terms of State
Concrete State Implementations
 In our example, we can have two states – one for turning TV on and another
to turn it off. So we will create two concrete state implementations for these
behaviors.
Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi
Infotech Park, Hinjawadi, Pune - 411 057
Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
14
Now we are ready to implement our Context object that will change it’s behavior
based on it’s internal state.
 Context Implementation
Solution to Problem in Terms of State
Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi
Infotech Park, Hinjawadi, Pune - 411 057
Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
15
 Test Program /Client Program
Now let’s write a simple program to test our implementation of TV Remote using
State pattern.
Solution to Problem in Terms of State
Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi
Infotech Park, Hinjawadi, Pune - 411 057
Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
16
The benefits of using State pattern to implement
polymorphic behavior is clearly visible,
the chances of error are less and it’s very easy to
add more states for additional behavior making it
more robust,
easily maintainable and flexible.
Also State pattern helped in avoiding if-else or
switch-case conditional logic in this scenario.
 Avoiding inconsistent states
Putting all associated behavior together in one
state object
Removes monolithic if or case statements
Advantages
Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi
Infotech Park, Hinjawadi, Pune - 411 057
Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
17
Implementation
Steps to implementation
Create an interface /create our state interface
Create concrete classes implementing the same interface.
Create Context Class. /set up a context
Use the Context to see change in behaviour when State
changes.
Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi
Infotech Park, Hinjawadi, Pune - 411 057
Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
18
Implementation
 We are going to create a State interface defining an action and concrete state
classes implementing the State interface.
 Context is a class which carries a State.
 StatePatternDemo, our demo class, will use Context and state objects to
demonstrate change in Context behavior based on type of state it is in.
Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi
Infotech Park, Hinjawadi, Pune - 411 057
Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
19
Implementation
Step 1
Create an interface.
State.java
Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi
Infotech Park, Hinjawadi, Pune - 411 057
Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
20
Implementation
Step 2
Create concrete classes implementing the same interface.
StartState.java
StopState.java
Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi
Infotech Park, Hinjawadi, Pune - 411 057
Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
21
Implementation
Step 3
Create Context Class.
Context.java
Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi
Infotech Park, Hinjawadi, Pune - 411 057
Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
22
Implementation
Step 4
Use the Context to see change in behaviour when State changes.
StatePatternDemo.java
Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi
Infotech Park, Hinjawadi, Pune - 411 057
Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
23
Step 5
Verify the output.
Implementation
Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi
Infotech Park, Hinjawadi, Pune - 411 057
Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
24
Applications
Where Would I Use This Pattern?
You should use the State pattern when the behaviour of
an object should be influenced by it's state, and when
complex conditions tie object behaviour to it's state.
Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi
Infotech Park, Hinjawadi, Pune - 411 057
Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
25
THANK YOU
For further information please feel free to contact
Dr. Ravi Patki
ravip@isquareit.edu.in
Department of Information Technology,
Hope Foundation’s International Institute of Information Technology, I²IT
P-14, Rajiv Gandhi Infotech Park, Hinjawadi, MIDC Phase I
Pune – 411057
Tel +91 20 22933441
www.isquareit.edu.in; info@isquareit.edu.in

More Related Content

What's hot

Systems Programming & Operating Systems - Overview of LEX-and-YACC
Systems Programming & Operating Systems - Overview of LEX-and-YACCSystems Programming & Operating Systems - Overview of LEX-and-YACC
Systems Programming & Operating Systems - Overview of LEX-and-YACC
International Institute of Information Technology (I²IT)
 
Difference Between AI(Artificial Intelligence), ML(Machine Learning), DL (Dee...
Difference Between AI(Artificial Intelligence), ML(Machine Learning), DL (Dee...Difference Between AI(Artificial Intelligence), ML(Machine Learning), DL (Dee...
Difference Between AI(Artificial Intelligence), ML(Machine Learning), DL (Dee...
International Institute of Information Technology (I²IT)
 
Introduction To Fog Computing
Introduction To Fog ComputingIntroduction To Fog Computing
Data Structure - Linked List
Data Structure - Linked ListData Structure - Linked List
Artificial Intelligence - Introduction
Artificial Intelligence - IntroductionArtificial Intelligence - Introduction
Artificial Intelligence - Introduction
International Institute of Information Technology (I²IT)
 
Basics of Computer Graphics
Basics of Computer GraphicsBasics of Computer Graphics
Functions in Python
Functions in PythonFunctions in Python
Factor Analysis & The Measurement Model
Factor Analysis & The Measurement Model Factor Analysis & The Measurement Model
Factor Analysis & The Measurement Model
International Institute of Information Technology (I²IT)
 
What Is Cascading Style Sheet?
What Is Cascading Style Sheet?What Is Cascading Style Sheet?
Adapter Pattern: Introduction & Implementation (with examples)
Adapter Pattern: Introduction & Implementation (with examples)Adapter Pattern: Introduction & Implementation (with examples)
Adapter Pattern: Introduction & Implementation (with examples)
International Institute of Information Technology (I²IT)
 
Importance of Theory of Computations
Importance of Theory of ComputationsImportance of Theory of Computations
Importance of Theory of Computations
International Institute of Information Technology (I²IT)
 
Programming with LEX & YACC
Programming with LEX & YACCProgramming with LEX & YACC
Differential Equation - Order Degree
Differential Equation - Order DegreeDifferential Equation - Order Degree
Differential Equation - Order Degree
International Institute of Information Technology (I²IT)
 
Introduction to TCP Protocol Suite
Introduction to TCP Protocol SuiteIntroduction to TCP Protocol Suite
What is DevOps?
What is DevOps?What is DevOps?
Database Query Optimization
Database Query OptimizationDatabase Query Optimization
Introduction to Wireless Sensor Networks (WSN)
Introduction to Wireless Sensor Networks (WSN)Introduction to Wireless Sensor Networks (WSN)
Introduction to Wireless Sensor Networks (WSN)
International Institute of Information Technology (I²IT)
 
What are the real differences between a wireframe, storyboard and a prototype?
What are the real differences between a wireframe, storyboard and a prototype?What are the real differences between a wireframe, storyboard and a prototype?
What are the real differences between a wireframe, storyboard and a prototype?
International Institute of Information Technology (I²IT)
 
DESIGN PATTERNS: Strategy Patterns
DESIGN PATTERNS: Strategy PatternsDESIGN PATTERNS: Strategy Patterns
Superstructure and it's various components
Superstructure and it's various componentsSuperstructure and it's various components
Superstructure and it's various components
International Institute of Information Technology (I²IT)
 

What's hot (20)

Systems Programming & Operating Systems - Overview of LEX-and-YACC
Systems Programming & Operating Systems - Overview of LEX-and-YACCSystems Programming & Operating Systems - Overview of LEX-and-YACC
Systems Programming & Operating Systems - Overview of LEX-and-YACC
 
Difference Between AI(Artificial Intelligence), ML(Machine Learning), DL (Dee...
Difference Between AI(Artificial Intelligence), ML(Machine Learning), DL (Dee...Difference Between AI(Artificial Intelligence), ML(Machine Learning), DL (Dee...
Difference Between AI(Artificial Intelligence), ML(Machine Learning), DL (Dee...
 
Introduction To Fog Computing
Introduction To Fog ComputingIntroduction To Fog Computing
Introduction To Fog Computing
 
Data Structure - Linked List
Data Structure - Linked ListData Structure - Linked List
Data Structure - Linked List
 
Artificial Intelligence - Introduction
Artificial Intelligence - IntroductionArtificial Intelligence - Introduction
Artificial Intelligence - Introduction
 
Basics of Computer Graphics
Basics of Computer GraphicsBasics of Computer Graphics
Basics of Computer Graphics
 
Functions in Python
Functions in PythonFunctions in Python
Functions in Python
 
Factor Analysis & The Measurement Model
Factor Analysis & The Measurement Model Factor Analysis & The Measurement Model
Factor Analysis & The Measurement Model
 
What Is Cascading Style Sheet?
What Is Cascading Style Sheet?What Is Cascading Style Sheet?
What Is Cascading Style Sheet?
 
Adapter Pattern: Introduction & Implementation (with examples)
Adapter Pattern: Introduction & Implementation (with examples)Adapter Pattern: Introduction & Implementation (with examples)
Adapter Pattern: Introduction & Implementation (with examples)
 
Importance of Theory of Computations
Importance of Theory of ComputationsImportance of Theory of Computations
Importance of Theory of Computations
 
Programming with LEX & YACC
Programming with LEX & YACCProgramming with LEX & YACC
Programming with LEX & YACC
 
Differential Equation - Order Degree
Differential Equation - Order DegreeDifferential Equation - Order Degree
Differential Equation - Order Degree
 
Introduction to TCP Protocol Suite
Introduction to TCP Protocol SuiteIntroduction to TCP Protocol Suite
Introduction to TCP Protocol Suite
 
What is DevOps?
What is DevOps?What is DevOps?
What is DevOps?
 
Database Query Optimization
Database Query OptimizationDatabase Query Optimization
Database Query Optimization
 
Introduction to Wireless Sensor Networks (WSN)
Introduction to Wireless Sensor Networks (WSN)Introduction to Wireless Sensor Networks (WSN)
Introduction to Wireless Sensor Networks (WSN)
 
What are the real differences between a wireframe, storyboard and a prototype?
What are the real differences between a wireframe, storyboard and a prototype?What are the real differences between a wireframe, storyboard and a prototype?
What are the real differences between a wireframe, storyboard and a prototype?
 
DESIGN PATTERNS: Strategy Patterns
DESIGN PATTERNS: Strategy PatternsDESIGN PATTERNS: Strategy Patterns
DESIGN PATTERNS: Strategy Patterns
 
Superstructure and it's various components
Superstructure and it's various componentsSuperstructure and it's various components
Superstructure and it's various components
 

Similar to State Pattern: Introduction & Implementation

What Is Smart Computing?
What Is Smart Computing?What Is Smart Computing?
Machine Learning: Bias and Variance Trade-off
Machine Learning: Bias and Variance Trade-offMachine Learning: Bias and Variance Trade-off
Machine Learning: Bias and Variance Trade-off
International Institute of Information Technology (I²IT)
 
ICFAI IT and Systems - Solved assignments and case study help
ICFAI IT and Systems  - Solved assignments and case study helpICFAI IT and Systems  - Solved assignments and case study help
ICFAI IT and Systems - Solved assignments and case study help
smumbahelp
 
IRJET - Application for Public Issues
IRJET -  	  Application for Public IssuesIRJET -  	  Application for Public Issues
IRJET - Application for Public Issues
IRJET Journal
 
Mobile Application of Pet Adoption System
Mobile Application of Pet Adoption SystemMobile Application of Pet Adoption System
Mobile Application of Pet Adoption System
IRJET Journal
 
Types of Artificial Intelligence
Types of Artificial Intelligence Types of Artificial Intelligence
Android Based E-Learning Application Class-E
Android Based E-Learning Application Class-EAndroid Based E-Learning Application Class-E
Android Based E-Learning Application Class-E
IRJET Journal
 
IRJET- IoT Enabled Children Safety System
IRJET- IoT Enabled Children Safety SystemIRJET- IoT Enabled Children Safety System
IRJET- IoT Enabled Children Safety System
IRJET Journal
 
Smart Closet Organizer
Smart Closet OrganizerSmart Closet Organizer
Agile IT: Modern Architecture for Rapid Mobile App Development
Agile IT: Modern Architecture for Rapid Mobile App DevelopmentAgile IT: Modern Architecture for Rapid Mobile App Development
Agile IT: Modern Architecture for Rapid Mobile App Development
AnyPresence
 
FINAL REVIEW for final semester internship.pptx
FINAL REVIEW for final semester internship.pptxFINAL REVIEW for final semester internship.pptx
FINAL REVIEW for final semester internship.pptx
royromeo560
 
Ai proposal
Ai proposalAi proposal
Ai proposal
zoniG
 
NaveenKumar_CV_NOV
NaveenKumar_CV_NOVNaveenKumar_CV_NOV
NaveenKumar_CV_NOVNaveen Kumar
 
2011B1A7689G-TrishuDey-Report
 2011B1A7689G-TrishuDey-Report 2011B1A7689G-TrishuDey-Report
2011B1A7689G-TrishuDey-ReportTrishu Dey
 
DAA Introduction to Algorithms & Application
DAA Introduction to Algorithms & ApplicationDAA Introduction to Algorithms & Application
DAA Introduction to Algorithms & Application
International Institute of Information Technology (I²IT)
 
IRJET - E-Wallet
IRJET - E-WalletIRJET - E-Wallet
IRJET - E-Wallet
IRJET Journal
 
WEB APPLICATION FOR MATHEMATICS CLUB OF P.C.E
WEB APPLICATION FOR MATHEMATICS CLUB OF P.C.EWEB APPLICATION FOR MATHEMATICS CLUB OF P.C.E
WEB APPLICATION FOR MATHEMATICS CLUB OF P.C.E
IRJET Journal
 

Similar to State Pattern: Introduction & Implementation (20)

What Is Smart Computing?
What Is Smart Computing?What Is Smart Computing?
What Is Smart Computing?
 
Machine Learning: Bias and Variance Trade-off
Machine Learning: Bias and Variance Trade-offMachine Learning: Bias and Variance Trade-off
Machine Learning: Bias and Variance Trade-off
 
ICFAI IT and Systems - Solved assignments and case study help
ICFAI IT and Systems  - Solved assignments and case study helpICFAI IT and Systems  - Solved assignments and case study help
ICFAI IT and Systems - Solved assignments and case study help
 
Umapathi_Resume
Umapathi_ResumeUmapathi_Resume
Umapathi_Resume
 
IRJET - Application for Public Issues
IRJET -  	  Application for Public IssuesIRJET -  	  Application for Public Issues
IRJET - Application for Public Issues
 
Mobile Application of Pet Adoption System
Mobile Application of Pet Adoption SystemMobile Application of Pet Adoption System
Mobile Application of Pet Adoption System
 
Types of Artificial Intelligence
Types of Artificial Intelligence Types of Artificial Intelligence
Types of Artificial Intelligence
 
Android Based E-Learning Application Class-E
Android Based E-Learning Application Class-EAndroid Based E-Learning Application Class-E
Android Based E-Learning Application Class-E
 
IRJET- IoT Enabled Children Safety System
IRJET- IoT Enabled Children Safety SystemIRJET- IoT Enabled Children Safety System
IRJET- IoT Enabled Children Safety System
 
Smart Closet Organizer
Smart Closet OrganizerSmart Closet Organizer
Smart Closet Organizer
 
Agile IT: Modern Architecture for Rapid Mobile App Development
Agile IT: Modern Architecture for Rapid Mobile App DevelopmentAgile IT: Modern Architecture for Rapid Mobile App Development
Agile IT: Modern Architecture for Rapid Mobile App Development
 
FINAL REVIEW for final semester internship.pptx
FINAL REVIEW for final semester internship.pptxFINAL REVIEW for final semester internship.pptx
FINAL REVIEW for final semester internship.pptx
 
Ai proposal
Ai proposalAi proposal
Ai proposal
 
Shubham_Bhaiya_Exp
Shubham_Bhaiya_ExpShubham_Bhaiya_Exp
Shubham_Bhaiya_Exp
 
Sagar BODAGE's CV
Sagar BODAGE's CVSagar BODAGE's CV
Sagar BODAGE's CV
 
NaveenKumar_CV_NOV
NaveenKumar_CV_NOVNaveenKumar_CV_NOV
NaveenKumar_CV_NOV
 
2011B1A7689G-TrishuDey-Report
 2011B1A7689G-TrishuDey-Report 2011B1A7689G-TrishuDey-Report
2011B1A7689G-TrishuDey-Report
 
DAA Introduction to Algorithms & Application
DAA Introduction to Algorithms & ApplicationDAA Introduction to Algorithms & Application
DAA Introduction to Algorithms & Application
 
IRJET - E-Wallet
IRJET - E-WalletIRJET - E-Wallet
IRJET - E-Wallet
 
WEB APPLICATION FOR MATHEMATICS CLUB OF P.C.E
WEB APPLICATION FOR MATHEMATICS CLUB OF P.C.EWEB APPLICATION FOR MATHEMATICS CLUB OF P.C.E
WEB APPLICATION FOR MATHEMATICS CLUB OF P.C.E
 

More from International Institute of Information Technology (I²IT)

Minimization of DFA
Minimization of DFAMinimization of DFA
Understanding Natural Language Processing
Understanding Natural Language ProcessingUnderstanding Natural Language Processing
Understanding Natural Language Processing
International Institute of Information Technology (I²IT)
 
Professional Ethics & Etiquette: What Are They & How Do I Get Them?
Professional Ethics & Etiquette: What Are They & How Do I Get Them?Professional Ethics & Etiquette: What Are They & How Do I Get Them?
Professional Ethics & Etiquette: What Are They & How Do I Get Them?
International Institute of Information Technology (I²IT)
 
Writing Skills: Importance of Writing Skills
Writing Skills: Importance of Writing SkillsWriting Skills: Importance of Writing Skills
Writing Skills: Importance of Writing Skills
International Institute of Information Technology (I²IT)
 
Professional Communication | Introducing Oneself
Professional Communication | Introducing Oneself Professional Communication | Introducing Oneself
Professional Communication | Introducing Oneself
International Institute of Information Technology (I²IT)
 
Servlet: A Server-side Technology
Servlet: A Server-side TechnologyServlet: A Server-side Technology
What Is Jenkins? Features and How It Works
What Is Jenkins? Features and How It WorksWhat Is Jenkins? Features and How It Works
What Is Jenkins? Features and How It Works
International Institute of Information Technology (I²IT)
 
Cloud Computing
Cloud ComputingCloud Computing
Hypothesis-Testing
Hypothesis-TestingHypothesis-Testing
Data Science, Big Data, Data Analytics
Data Science, Big Data, Data AnalyticsData Science, Big Data, Data Analytics
Data Science, Big Data, Data Analytics
International Institute of Information Technology (I²IT)
 
Sentiment Analysis in Machine Learning
Sentiment Analysis in  Machine LearningSentiment Analysis in  Machine Learning
Sentiment Analysis in Machine Learning
International Institute of Information Technology (I²IT)
 
What Is Cloud Computing?
What Is Cloud Computing?What Is Cloud Computing?
Java as Object Oriented Programming Language
Java as Object Oriented Programming LanguageJava as Object Oriented Programming Language
Java as Object Oriented Programming Language
International Institute of Information Technology (I²IT)
 
Data Visualization - How to connect Microsoft Forms to Power BI
Data Visualization - How to connect Microsoft Forms to Power BIData Visualization - How to connect Microsoft Forms to Power BI
Data Visualization - How to connect Microsoft Forms to Power BI
International Institute of Information Technology (I²IT)
 
AVL Tree Explained
AVL Tree ExplainedAVL Tree Explained
Yoga To Fight & Win Against COVID-19
Yoga To Fight & Win Against COVID-19Yoga To Fight & Win Against COVID-19
Yoga To Fight & Win Against COVID-19
International Institute of Information Technology (I²IT)
 
LR(0) PARSER
LR(0) PARSERLR(0) PARSER
Land Pollution - Causes, Effects & Solution
Land Pollution - Causes, Effects & SolutionLand Pollution - Causes, Effects & Solution
Land Pollution - Causes, Effects & Solution
International Institute of Information Technology (I²IT)
 
Supervised Learning in Cybersecurity
Supervised Learning in CybersecuritySupervised Learning in Cybersecurity
Supervised Learning in Cybersecurity
International Institute of Information Technology (I²IT)
 
Sampling Theorem and Band Limited Signals
Sampling Theorem and Band Limited SignalsSampling Theorem and Band Limited Signals
Sampling Theorem and Band Limited Signals
International Institute of Information Technology (I²IT)
 

More from International Institute of Information Technology (I²IT) (20)

Minimization of DFA
Minimization of DFAMinimization of DFA
Minimization of DFA
 
Understanding Natural Language Processing
Understanding Natural Language ProcessingUnderstanding Natural Language Processing
Understanding Natural Language Processing
 
Professional Ethics & Etiquette: What Are They & How Do I Get Them?
Professional Ethics & Etiquette: What Are They & How Do I Get Them?Professional Ethics & Etiquette: What Are They & How Do I Get Them?
Professional Ethics & Etiquette: What Are They & How Do I Get Them?
 
Writing Skills: Importance of Writing Skills
Writing Skills: Importance of Writing SkillsWriting Skills: Importance of Writing Skills
Writing Skills: Importance of Writing Skills
 
Professional Communication | Introducing Oneself
Professional Communication | Introducing Oneself Professional Communication | Introducing Oneself
Professional Communication | Introducing Oneself
 
Servlet: A Server-side Technology
Servlet: A Server-side TechnologyServlet: A Server-side Technology
Servlet: A Server-side Technology
 
What Is Jenkins? Features and How It Works
What Is Jenkins? Features and How It WorksWhat Is Jenkins? Features and How It Works
What Is Jenkins? Features and How It Works
 
Cloud Computing
Cloud ComputingCloud Computing
Cloud Computing
 
Hypothesis-Testing
Hypothesis-TestingHypothesis-Testing
Hypothesis-Testing
 
Data Science, Big Data, Data Analytics
Data Science, Big Data, Data AnalyticsData Science, Big Data, Data Analytics
Data Science, Big Data, Data Analytics
 
Sentiment Analysis in Machine Learning
Sentiment Analysis in  Machine LearningSentiment Analysis in  Machine Learning
Sentiment Analysis in Machine Learning
 
What Is Cloud Computing?
What Is Cloud Computing?What Is Cloud Computing?
What Is Cloud Computing?
 
Java as Object Oriented Programming Language
Java as Object Oriented Programming LanguageJava as Object Oriented Programming Language
Java as Object Oriented Programming Language
 
Data Visualization - How to connect Microsoft Forms to Power BI
Data Visualization - How to connect Microsoft Forms to Power BIData Visualization - How to connect Microsoft Forms to Power BI
Data Visualization - How to connect Microsoft Forms to Power BI
 
AVL Tree Explained
AVL Tree ExplainedAVL Tree Explained
AVL Tree Explained
 
Yoga To Fight & Win Against COVID-19
Yoga To Fight & Win Against COVID-19Yoga To Fight & Win Against COVID-19
Yoga To Fight & Win Against COVID-19
 
LR(0) PARSER
LR(0) PARSERLR(0) PARSER
LR(0) PARSER
 
Land Pollution - Causes, Effects & Solution
Land Pollution - Causes, Effects & SolutionLand Pollution - Causes, Effects & Solution
Land Pollution - Causes, Effects & Solution
 
Supervised Learning in Cybersecurity
Supervised Learning in CybersecuritySupervised Learning in Cybersecurity
Supervised Learning in Cybersecurity
 
Sampling Theorem and Band Limited Signals
Sampling Theorem and Band Limited SignalsSampling Theorem and Band Limited Signals
Sampling Theorem and Band Limited Signals
 

Recently uploaded

DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
Product School
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
Elena Simperl
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
Cheryl Hung
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Product School
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
Sri Ambati
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
Product School
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Elena Simperl
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
g2nightmarescribd
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 

Recently uploaded (20)

DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 

State Pattern: Introduction & Implementation

  • 1. 1 DESIGN PATTERNS : State Pattern By RAVI P. PATKI Associate Professor (IT) Hope Foundation’s International Institute of Information Technology
  • 2. 2 Agenda  Introduction  Problem in Software Design /code  Definition of State Pattern  Solution to Problem in terms of Strategy Pattern  Advantages  Implementation of State Pattern  Applications Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi Infotech Park, Hinjawadi, Pune - 411 057 Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
  • 3. 3 Introduction State pattern is one of the behavioral design pattern. State design pattern is used when an Object change it’s behavior based on it’s internal state. The state pattern is a behavioral software design pattern that implements a state machine in an object-oriented way. With the state pattern, a state machine is implemented by implementing each individual state as a derived class of the state pattern interface, and implementing state transitions by invoking methods defined by the pattern's super class. Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi Infotech Park, Hinjawadi, Pune - 411 057 Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
  • 4. 4  This pattern is used in computer programming to encapsulate varying behavior for the same object based on its internal state.  This can be a cleaner way for an object to change its behavior at runtime without resorting to large monolithic /huge conditional statements and thus improve maintainability. Introduction Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi Infotech Park, Hinjawadi, Pune - 411 057 Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
  • 5. 5 Example  The State pattern allows an object to change its behavior when its internal state changes. This pattern can be observed in a vending machine.  Vending machines have states based on the inventory, amount of currency deposited, the ability to make change, the item selected, etc.  When currency is deposited and a selection is made, a vending machine will either deliver a product and no change, deliver a product and change, deliver no product due to insufficient currency on deposit, or deliver no product due to inventory depletion. Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi Infotech Park, Hinjawadi, Pune - 411 057 Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
  • 6. 6 Problem in Software Design  Tired of conditionals?  When writing code, our classes often go through a series of transformations.  What starts out as a simple class will grow as behavior is added.  if you didn’t take the necessary precautions, your code will become difficult to understand and maintain.  Too often, the state of an object is kept by creating multiple Boolean attributes and deciding how to behave based on the values.  This can become cumbersome and difficult to maintain when the complexity of your class starts to increase.  This is a common problem on most projects. Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi Infotech Park, Hinjawadi, Pune - 411 057 Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
  • 7. 7 Suppose we want to implement a TV Remote object with a simple button to perform action, if the State is ON, it will turn on the TV and if state is OFF, it will turn off the TV. We can implement it using if-else condition like below; Problem in Software Design Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi Infotech Park, Hinjawadi, Pune - 411 057 Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
  • 8. 8 Problem in Software Design What are the problems with above design? Notice that  client code should know the specific values to use for setting the state of remote,  further more if number of states increase then the tight coupling between implementation and the client code will be very hard to maintain and extend.  How can we avoid this?  Design / Code will become more cleaner by using Enums and Switches or multiple if then else (Similar to our SavingAccount object in Assignment no.3)  Here in this solutions Instead of a bunch of flags, we will just have one state_ field. We also flip the order of our branching.  But again code become monolithic or as single block.  This is a common problem on most projects, and it is wise to model it with a Finite State Machine.  In fact, there is a design pattern called State that address this very well, so you can find hundreds of gems implementing this pattern. Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi Infotech Park, Hinjawadi, Pune - 411 057 Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
  • 9. 9 Definition : State Pattern  The State pattern is known as a behavioural pattern - it's used to manage algorithms, relationships and responsibilities between objects.  In State pattern a class behavior changes based on its state. This type of design pattern comes under behavior pattern.  In State pattern, we create objects which represent various states and a context object whose behavior varies as its state object changes.  The definition of State provided in the original Gang of Four book on Design Patterns states: “Allows an object to alter its behaviour when its internal state changes. The object will appear to change its class.” Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi Infotech Park, Hinjawadi, Pune - 411 057 Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
  • 10. 10 Definition : State Pattern Let's take a look at the diagram definition The Context can have a number of internal States, whenever the request() method is called on the Context, the message is delegated to the State to handle. The State interface defines a common interface for all concrete states, encapsulating all behaviour associated with a particular state. The ConcreteState implements it's own implementation for the request. When a Context changes state, what really happens is that we have a different ConcreteState associated with it. Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi Infotech Park, Hinjawadi, Pune - 411 057 Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
  • 11. 11 Definition : State Pattern Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi Infotech Park, Hinjawadi, Pune - 411 057 Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
  • 12. 12 Solution to Problem in Terms of State State Interface First of all we will create State interface that will define the method that should be implemented by different concrete states and context class. Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi Infotech Park, Hinjawadi, Pune - 411 057 Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
  • 13. 13 Solution to Problem in Terms of State Concrete State Implementations  In our example, we can have two states – one for turning TV on and another to turn it off. So we will create two concrete state implementations for these behaviors. Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi Infotech Park, Hinjawadi, Pune - 411 057 Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
  • 14. 14 Now we are ready to implement our Context object that will change it’s behavior based on it’s internal state.  Context Implementation Solution to Problem in Terms of State Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi Infotech Park, Hinjawadi, Pune - 411 057 Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
  • 15. 15  Test Program /Client Program Now let’s write a simple program to test our implementation of TV Remote using State pattern. Solution to Problem in Terms of State Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi Infotech Park, Hinjawadi, Pune - 411 057 Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
  • 16. 16 The benefits of using State pattern to implement polymorphic behavior is clearly visible, the chances of error are less and it’s very easy to add more states for additional behavior making it more robust, easily maintainable and flexible. Also State pattern helped in avoiding if-else or switch-case conditional logic in this scenario.  Avoiding inconsistent states Putting all associated behavior together in one state object Removes monolithic if or case statements Advantages Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi Infotech Park, Hinjawadi, Pune - 411 057 Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
  • 17. 17 Implementation Steps to implementation Create an interface /create our state interface Create concrete classes implementing the same interface. Create Context Class. /set up a context Use the Context to see change in behaviour when State changes. Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi Infotech Park, Hinjawadi, Pune - 411 057 Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
  • 18. 18 Implementation  We are going to create a State interface defining an action and concrete state classes implementing the State interface.  Context is a class which carries a State.  StatePatternDemo, our demo class, will use Context and state objects to demonstrate change in Context behavior based on type of state it is in. Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi Infotech Park, Hinjawadi, Pune - 411 057 Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
  • 19. 19 Implementation Step 1 Create an interface. State.java Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi Infotech Park, Hinjawadi, Pune - 411 057 Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
  • 20. 20 Implementation Step 2 Create concrete classes implementing the same interface. StartState.java StopState.java Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi Infotech Park, Hinjawadi, Pune - 411 057 Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
  • 21. 21 Implementation Step 3 Create Context Class. Context.java Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi Infotech Park, Hinjawadi, Pune - 411 057 Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
  • 22. 22 Implementation Step 4 Use the Context to see change in behaviour when State changes. StatePatternDemo.java Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi Infotech Park, Hinjawadi, Pune - 411 057 Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
  • 23. 23 Step 5 Verify the output. Implementation Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi Infotech Park, Hinjawadi, Pune - 411 057 Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
  • 24. 24 Applications Where Would I Use This Pattern? You should use the State pattern when the behaviour of an object should be influenced by it's state, and when complex conditions tie object behaviour to it's state. Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi Infotech Park, Hinjawadi, Pune - 411 057 Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
  • 25. 25 THANK YOU For further information please feel free to contact Dr. Ravi Patki ravip@isquareit.edu.in Department of Information Technology, Hope Foundation’s International Institute of Information Technology, I²IT P-14, Rajiv Gandhi Infotech Park, Hinjawadi, MIDC Phase I Pune – 411057 Tel +91 20 22933441 www.isquareit.edu.in; info@isquareit.edu.in