By : Atri Chatterjee Souvik Ghosh

Slides:



Advertisements
Similar presentations
Understand and appreciate Object Oriented Programming (OOP) Objects are self-contained modules or subroutines that contain data as well as the functions.
Advertisements

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.
Matt Klein. Decorator Pattern  Intent  Attach Additional responsibilities to an object by dynamically. Decorators provide a flexible alternative to.
Classes & Objects Computer Science I Last updated 9/30/10.
Matt Klein 7/6/2009.  Behavioral Pattern  Intent  Allow an object to alter its behavior when its internal state changes. The object will appear to.
CBSD – Component Based Software Development - Introduction -
Bridge The decoupling of abstraction and implementation.
Patterns Lecture 2. Singleton Ensure a class only has one instance, and provide a global point of access to it.
BehavioralCmpE196G1 Behavioral Patterns Chain of Responsibility (requests through a chain of candidates) Command (encapsulates a request) Interpreter (grammar.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Command Pattern Chihung Liao Cynthia Jiang. Waiter Order Execute() Hamburger Execute() Hot Dogs Execute() Fries Execute() Cook Make Food()
Chapter 10 Classes Continued
Interpreter By: Mahmoodreza Jahanseir Amirkabir University of Technology Computer Engineering Department Fall 2010.
Inheritance and Polymorphism CS351 – Programming Paradigms.
C++ fundamentals.
Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003.
BDP Behavioral Pattern. BDP-2 Behavioral Patters Concerned with algorithms & assignment of responsibilities Patterns of Communication between Objects.
Design Patterns.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Design patterns. What is a design pattern? Christopher Alexander: «The pattern describes a problem which again and again occurs in the work, as well as.
Structural Pattern: Decorator There are times when the use of subclasses to modify the behavior of individual objects is problematic. C h a p t e r 4.
CS 325: Software Engineering March 17, 2015 Applying Patterns (Part A) The Façade Pattern The Adapter Pattern Interfaces & Implementations The Strategy.
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
©Fraser Hutchinson & Cliff Green C++ Certificate Program C++ Intermediate Decorator, Strategy, State Patterns.
Design engineering Vilnius The goal of design engineering is to produce a model that exhibits: firmness – a program should not have bugs that inhibit.
Chapter 2 Introducing Interfaces Summary prepared by Kirk Scott.
Behavioral Design Patterns Morteza Yousefi University Of Science & Technology Of Mazandaran 1of 27Behavioral Design Patterns.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns IX Interpreter, Mediator, Template Method recap.
Encapsulation COMP 401, Fall 2014 Lecture 06 9/4/2014.
3-1 State Design Pattern C Sc 335 Rick Mercer. State Design Pattern State is one of the Behavioral patterns It is similar to Strategy Allows an object.
Unit 4 Object-Oriented Design Patterns NameStudent Number CAI XIANGHT082182A KYAW THU LINHT082238Y LI PENGFEIHT082220L NAUNG NAUNG LATTHT082195L PLATHOTTAM.
Designing Classes Prelude © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank.
What is Object-Oriented?  Organization of software as a collection of discreet objects that incorporate both data structure and behavior.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VIII Chain of Responsibility, Strategy, State.
Creational Pattern: Factory Method At times, a framework is needed to standardize the behavior of objects that are used in a range of applications, while.
Prototype pattern Participants Prototype (Graphic) – declared an interface for cloning itself ConcretePrototype (EditBox, Slider) – implements an operation.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
The Strategy Pattern SE-2811 Dr. Mark L. Hornick 1.
Behavioral Patterns1 Nour El Kadri SEG 3202 Software Design and Architecture Notes based on U of T Design Patterns class.
Design Patterns Introduction
State Design Pattern. Behavioral Pattern Allows object to alter its behavior when internal state changes Uses Polymorphism to define different behaviors.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
The State Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
STATE PATTERN Presented by Bharavi Mishra Bharavi Mishraise
CS 325: Software Engineering March 19, 2015 Applying Patterns (Part B) Code Smells The Decorator Pattern The Observer Pattern The Template Method Pattern.
COP 4331 – OOD&P Lecture 7 Object Concepts. What is an Object Programming language definition: An instance of a class Design perspective is different.
The State Design Pattern A behavioral design pattern. Shivraj Persaud
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Chapter 11: Abstract Data Types Lecture # 17. Chapter 11 Topics The Concept of Abstraction Advantages of Abstract Data Types Design Issues for Abstract.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Design Patterns: MORE Examples
Strategy Design Pattern
Introduction to Design Patterns
Software Design and Architecture
OOP What is problem? Solution? OOP
State pattern – A logical ‘Finite State Machine’
Object Oriented Analysis and Design
Advanced Programming Behnam Hatami Fall 2017.
State Design Pattern 1.
Introduction to Behavioral Patterns (2)
Decorator Pattern Richard Gesick.
Classes & Objects – Revisited…
Interpreter Pattern.
DESIGN PATTERNS : State Pattern
Structural Patterns: Adapter and Bridge
CS 325: Software Engineering
State Design Pattern.
Presentation transcript:

By : Atri Chatterjee Souvik Ghosh STATE Pattern By : Atri Chatterjee Souvik Ghosh

General Description A type of Behavioral pattern. Allows an object to alter its behavior when its internal state changes. The object will appear to change its class. Uses Polymorphism to define different behaviors for different states of an object.

When to use STATE pattern ? State pattern is useful when there is an object that can be in one of several states, with different behavior in each state. To simplify operations that have large conditional statements that depend on the object’s state. if (myself = happy) then { eatIceCream(); …. } else if (myself = sad) then goToPub(); else if (myself = ecstatic) then

Example I water StateOfWater state variable WaterVapor LiquidWater Ice increaseTemp() decreaseTemp() increaseTemp() decreaseTemp() WaterVapor LiquidWater Ice Client increaseTemp() increaseTemp() decreaseTemp() increaseTemp() decreaseTemp() increaseTemp() decreaseTemp()

How is STATE pattern implemented ? “Context” class: Represents the interface to the outside world. “State” abstract class: Base class which defines the different states of the “state machine”. “Derived” classes from the State class: Defines the true nature of the state that the state machine can be in. Context class maintains a pointer to the current state. To change the state of the state machine, the pointer needs to be changed.

Example II MyMood MoodState state variable mad angry happy Client doSomething() mad angry happy Client doSomething() doSomething() doSomething() doSomething()

Benefits of using STATE pattern Localizes all behavior associated with a particular state into one object. New state and transitions can be added easily by defining new subclasses. Simplifies maintenance. It makes state transitions explicit. Separate objects for separate states makes transition explicit rather than using internal data values to define transitions in one combined object. State objects can be shared. Context can share State objects if there are no instance variables.

Food for thought… To have a monolithic single class or many subclasses ? Increases the number of classes and is less compact. Avoids large conditional statements. Where to define the state transitions ? If criteria is fixed, transition can be defined in the context. More flexible if transition is specified in the State subclass. Introduces dependencies between subclasses. Whether to create State objects as and when required or to create-them-once-and-use-many-times ? First is desirable if the context changes state infrequently. Later is desirable if the context changes state frequently.