DESIGN PATTERNS : State Pattern

Slides:



Advertisements
Similar presentations
GoF State Pattern Aaron Jacobs State(305) Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.
Advertisements

CS 350 – Software Design The Bridge Pattern – Chapter 10 Most powerful pattern so far. Gang of Four Definition: Decouple an abstraction from its implementation.
BehavioralCmpE196G1 Behavioral Patterns Chain of Responsibility (requests through a chain of candidates) Command (encapsulates a request) Interpreter (grammar.
Software Engineering I Object-Oriented Design Software Design Refinement Using Design Patterns Instructor: Dr. Hany H. Ammar Dept. of Computer Science.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
What Is a Factory Pattern?.  Factories are classes that create or construct something.  In the case of object-oriented code languages, factories construct.
Software Design Refinement Using Design Patterns Instructor: Dr. Hany H. Ammar Dept. of Computer Science and Electrical Engineering, WVU.
“A labor of love” (Multicast Pattern) Chapter 4 (pages or ) Chris Gordon.
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
CS 350 – Software Design The Strategy Pattern – Chapter 9 Changes to software, like other things in life, often focus on the immediate concerns and ignore.
Behavioral Design Patterns Morteza Yousefi University Of Science & Technology Of Mazandaran 1of 27Behavioral Design Patterns.
Unit 4 Object-Oriented Design Patterns NameStudent Number CAI XIANGHT082182A KYAW THU LINHT082238Y LI PENGFEIHT082220L NAUNG NAUNG LATTHT082195L PLATHOTTAM.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VIII Chain of Responsibility, Strategy, State.
Design Patterns -- Omkar. Introduction  When do we use design patterns  Uses of design patterns  Classification of design patterns  Creational design.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
Behavioral Patterns1 Nour El Kadri SEG 3202 Software Design and Architecture Notes based on U of T Design Patterns class.
The State Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
CS 350 – Software Design The Decorator Pattern – Chapter 17 In this chapter we expand our e-commerce case study and learn how to use the Decorator Pattern.
The State Design Pattern A behavioral design pattern. Shivraj Persaud
Sadegh Aliakbary Sharif University of Technology Fall 2010.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Design Patterns: MORE Examples
Software Design Refinement Using Design Patterns
Strategy: A Behavioral Design Pattern
Unit II-Chapter No. : 5- design Patterns
Strategy Design Pattern
MPCS – Advanced java Programming
CS 350 – Software Design The Strategy Pattern – Chapter 9
Unified Modeling Language
Behavioral Design Patterns
Instructor: Dr. Hany H. Ammar
Presented by Igor Ivković
State Design Pattern 1.
Introduction to Behavioral Patterns (2)
DESIGN PATTERNS : Strategy Pattern
COMPUTER NETWORK TECHNOLOGY
DESIGN PATTERNS : Introduction
Design pattern Lecture 9.
Physics of Nanoparticles
Composition & Resolution of Forces
State Pattern By the Group of Four.
Strategy Design Pattern
Introduction to Design Patterns
DESIGN PATTERNS : Adapter Pattern
Usability Heuristics Prof
NP-COMPLETE Prof. Manjusha Amritkar Assistant Professor Department of Information Technology Hope Foundation’s International Institute of Information.
International Institute of Information Technology, (I²IT).
Engineering Mathematics-I Eigenvalues and Eigenvectors
Software Design Methodologies and Testing
Pass Structure of Assembler
INTERNATIONAL INSTITUTE OF IFORMATION TECHNOLOGY, (I²IT)
INTERNATIONAL INSTITUTE OF IFORMATION TECHNOLOGY, (I²IT)
Newton’s Laws of Motion
By Sandeep Patil, Department of Computer Engineering, I²IT
Differential Equation
BINARY HEAP Prof ajitkumar shitole Assistant Professor Department of computer engineering Hope Foundation’s International Institute of Information.
Pass Structure of Assembler
gyroscope Prof Varsha Degaonkar Assistant Professor
Hope Foundation’s International Institute of Information.
Essay Writing Writing is an art Prepared by Vaidehi Banerjee
Introduction to Human Computer Interaction
Design procedure for an Integrator
Presented by Igor Ivković
Prof. Deptii Chaudhari.
Tel Hope Foundation’s International Institute of Information Technology, (I²IT). Tel
PIC Microcontroller ADC interfacing Prof. Ashvini Kulkarni
DAA - Introduction Dr. Sashikala Mishra Associate Professor Department of Computer Engineering Hope Foundation’s INTERNATIONAL INSTITUTE OF INFORMATION.
Tel Hope Foundation’s International Institute of Information Technology, (I²IT). Tel
Transaction Serializability
Presentation transcript:

DESIGN PATTERNS : State Pattern By RAVI P. PATKI Associate Professor (IT) Hope Foundation’s International Institute of Information Technology

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

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

Introduction 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. 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

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

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

Problem in Software Design 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; 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

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

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

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

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

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

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

Solution to Problem in Terms of State Now we are ready to implement our Context object that will change it’s behavior based on it’s internal state. Context 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

Solution to Problem in Terms of State Test Program /Client Program Now let’s write a simple program to test our implementation of TV Remote using 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

Advantages 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 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

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

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

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

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

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

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

Implementation Step 5 Verify the output. 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

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

THANK YOU For further information please feel free to contact Dr 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