State Pattern By the Group of Four.

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

Introduction to TouchDevelop
Lab Course / Praktikum: S ummer Semester 2012 Project Management and Software Development for Medical Applications Computer Aided Medical Procedures (CAMP),
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VIII Chain of Responsibility, Strategy, State.
The State Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
STATE PATTERN Presented by Bharavi Mishra Bharavi Mishraise
The State Design Pattern A behavioral design pattern. Shivraj Persaud
Slide design: Dr. Mark L. Hornick
Color Wheel/Color Theory
Summary prepared by Kirk Scott
Software Design Refinement Using Design Patterns
An Introduction to VEX IQ Programming with Modkit
Laura had a bag of marshmallows
Your teacher has announced that the class will be taking a field trip
CS101 Introduction to Computing Lecture 19 Programming Languages
Your Project Title Here
Rule: double the number and add 1
LearnZillion Notes: --This is your hook. Start with a question to draw the student in. We want that student saying, “huh, how do you do X?” Try to be specific.
LearnZillion Notes: --This is your hook. Start with a question to draw the student in. We want that student saying, “huh, how do you do X?” Try to be specific.
Procedural Abstraction Object-Oriented Code
LearnZillion Notes: --This is your hook. Start with a question to draw the student in. We want that student saying, “huh, how do you do X?” Try to be specific.
LearnZillion Notes: --This is your hook. Start with a question to draw the student in. We want that student saying, “huh, how do you do X?” Try to be specific.
LearnZillion Notes: --This is your hook. Start with a question to draw the student in. We want that student saying, “huh, how do you do X?” Try to be specific.
For example, how can you use exponents to write
7 x 2 5 LearnZillion Notes: --This is your hook. Start with a question to draw the student in. We want that student saying, “huh, how do you do X?” Try.
3. Object-oriented Design
State Design Pattern 1.
LearnZillion Notes: --This is your hook. Start with a question to draw the student in. We want that student saying, “huh, how do you do X?” Try to be specific.
LearnZillion Notes: --This is your hook. Start with a question to draw the student in. We want that student saying, “huh, how do you do X?” Try to be specific.
LearnZillion Notes: --This is our lesson objective. Keep it as short and student-friendly as possible. Put what they will learn in green and then how they’ll.
LearnZillion Notes: --This is your hook. Start with a question to draw the student in. We want that student saying, “huh, how do you do X?” Try to be specific.
LearnZillion Notes: --This is your hook. Start with a question to draw the student in. We want that student saying, “huh, how do you do X?” Try to be specific.
LearnZillion Notes: --This is your hook. Start with a question to draw the student in. We want that student saying, “huh, how do you do X?” Try to be specific.
Object Oriented Design Patterns - Creational Patterns
LearnZillion Notes: --This is your hook. Start with a question to draw the student in. We want that student saying, “huh, how do you do X?” Try to be specific.
CISC101 Reminders Assn 3 due Friday, this week. Quiz 3 next week.
LearnZillion Notes: --This is your hook. Start with a question to draw the student in. We want that student saying, “huh, how do you do X?” Try to be specific.
LearnZillion Notes: --This is your hook. Start with a question to draw the student in. We want that student saying, “huh, how do.
How do you determine an author’s purpose? LearnZillion Notes:
LearnZillion Notes: --This is your hook. Start with a question to draw the student in. We want that student saying, “huh, how do you do X?” Try to be specific.
For example, if you have 3 4 of a giant candy bar and decide to eat 1 6 of it, how much of the candy bar will you be eating? LearnZillion Notes: --This.
LearnZillion Notes: --This is your hook. Start with a question to draw the student in. We want that student saying, “huh, how do you do X?” Try to be specific.
Object oriented analysis and design
LearnZillion Notes: --This is your hook. Start with a question to draw the student in. We want that student saying, “huh, how do you do X?” Try to be specific.
LearnZillion Notes: --This is your hook. Start with a question to draw the student in. We want that student saying, “huh, how do you do X?” Try to be specific.
LearnZillion Notes: --This is your hook. Start with a question to draw the student in. We want that student saying, “huh, how do you do X?” Try to be specific.
DESIGN PATTERNS : Strategy Pattern
DESIGN PATTERNS : State Pattern
Design pattern Lecture 9.
Marcus Buckingham has presented a challenge to change the we think about Leadership, Managing and Sustained Individual Success. I would like to conduct.
LearnZillion Notes: --This is your hook. Start with a question to draw the student in. We want that student saying, “huh, how do you do X?” Try to be specific.
GRAPHICAL USER INTERFACE GITAM GADTAULA. OVERVIEW What is Human Computer Interface (User Interface) principles of user interface design What makes a good.
GRAPHICAL USER INTERFACE GITAM GADTAULA KATHMANDU UNIVERSITY CLASS PRESENTATION.
Basic OOP Concepts and Terms
Review of Previous Lesson
LearnZillion Notes: --This is your hook. Start with a question to draw the student in. We want that student saying, “huh, how do you do X?” Try to be specific.
A.
LearnZillion Notes: --This is your hook. Start with a question to draw the student in. We want that student saying, “huh, how do you do X?” Try to be specific.
For Example: Write a letter to your teacher explaining where you would like to go on a field trip and why. LearnZillion Notes: --This is your hook. Start.
LearnZillion Notes: --This is your hook. Start with a question to draw the student in. We want that student saying, “huh, how do you do X?” Try to be specific.
Give 6 different extension strategies and explain the problems that businesses might face implementing them. What’s happening here and what potential problems.
LearnZillion Notes: --This is your hook. Start with a question to draw the student in. We want that student saying, “huh, how do you do X?” Try to be specific.
x ft 30 ft 40 ft LearnZillion Notes:
LearnZillion Notes: --This is your hook. Start with a question to draw the student in. We want that student saying, “huh, how do you do X?” Try to be specific.
1 2 ÷ 4 LearnZillion Notes: --This is your hook. Start with a question to draw the student in. We want that student saying, “huh, how do you do X?”
LearnZillion Notes: --This is your hook. Start with a question to draw the student in. We want that student saying, “huh, how do you do X?” Try to be specific.
HFOOAD Chapter 5 Interlude
For example: How do you show an increase of 12% over the original cost, if the original cost is $x? LearnZillion Notes: --This is your hook. Start with.
I wonder…2 _ 1 2 ? LearnZillion Notes:
LearnZillion Notes: --This is your hook. Start with a question to draw the student in. We want that student saying, “huh, how do you do X?” Try to be specific.
Presentation transcript:

State Pattern By the Group of Four

State in a Day Life Think of it a little bit, many things in our life have states. Elevator : even an elevator has states Push up or down button Waiting for the elevator to arrive Door open Etc.

JAVA in real-life Many machines or scenarios in your life can also be implement with Programming too. Such as The Elevator Bank Account Etc.

JAVA in real-life (cont.) And many those stuff also have states How can we implement the state of thing in Object Oriented Design with quality? There are many ways to do it But the best approch is the State Pattern!!!

Introduction State Pattern is one of the 23 Patterns of Design Patterns by the Gang of Four State Pattern is a Behavioral Pattern(we will tell you later why). We, the Group of Four are the subclass of them and going to be their leaf to explain and illustrate the State Patterns to you.

Let’s try some example As we told you before that object in real-life also has state but to illustrate we would like to show you an easy test with the Graphic user interface. Why? To give you the idea of how the state change in the GUI.

Example Consider a class with next() and previous() whose behavior changes depend on the state of the object. The next() operation will change the state of the canvas object to the next color. The previous() operation will change the state of the canvas object to the previous color

This is what the UI look like

Example(cont.) Here is how the color state cycle. Now you can implement this class. Red Blue Black Green

Try it without State Pattern To implement this without a state pattern is very easy. You are giving part of a dummy context with part of a GUI. In the GUI you only need to implement only the action call nextState() and previousState.

Here is one of the solution

What is the problem? There is no problem with it if it only has 4 state and a tiny size of code. What if The code to implement one state are huge Let just say that some may have 20 state Man!! You ‘re probably die from coding and managing the change request.

Here is where State pattern come in Now I would like to introduce you to state pattern. Using this pattern will solve this problem for you.

State pattern Intent Allow an object to alter its behavior when its internal state changes. The object will appear to change its class. That ‘s why it’s a behavioral pattern because it allow object to alter its behavior Let’s see how.

Structure First please try to create the UML class diagram for the previous example with state pattern. Try it your way then your can come up and show it to the entire class. The structure is given in the next slide.

The Structure of state pattern

Class Diagram

Implemantation Here come the exercise2 You are giving the part of Context , GUI , concreteState class. The instruction is on the top Go implement it.

Solution

When to use State Pattern An object's behavior depends on its state, and it must change its behavior at run-time depending on that state. Operations have large, multipart conditional statements that depend on the object's state. The State pattern puts each branch of the conditional in a separate class.

Advantages of State pattern Localized the behavior of each state into its own class. Remove all duplicate if statements. Flexible Potential extensibility In some application it increase testability.

Disadvantage The only disadvantage of this pattern is that is you have more state say 100 state you will have 100 ConcreteState class too.

Thank you Now you know the State pattern I hope this presentation help you in some way If there any mistakes I would like to apologise for those.