diff --git a/README.md b/README.md index 9e0034c..be827ac 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,32 @@ -# DesignPatternsJava9 -This repo consists Gang of Four Design patterns code on Java 9. Each branch in the repository has code of 1 design pattern. Switch repository to try out different design patterns. +# What is State Design Pattern +State pattern allows an object to behave differently depending on its internal state. + +## Diagram +![Diagram](https://github.com/premaseem/DesignPatternsJava9/blob/state-pattern/diagrams/State-Design-Pattern-class-diagram.jpeg "Diagram") + +![Diagram](https://github.com/premaseem/DesignPatternsJava9/blob/state-pattern/diagrams/State-Design-Pattern-generic.jpeg "Diagram") + +![Diagram](https://github.com/premaseem/DesignPatternsJava9/blob/state-pattern/diagrams/StatePatternSequenceDiagram.png "Diagram") + +### When to use State Design 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. + +### Learn Design Patterns with Java by Aseem Jain +This repository contains working project code used in video Course by Packt Publication with title "Learn Design Patterns with Java " authored by "Aseem Jain". + +### Course link: +https://www.packtpub.com/application-development/learn-design-patterns-java-9-video + +### ![ http://in.linkedin.com/in/premaseem](https://github.com/premaseem/DesignPatternsJava9/blob/master/linkedin.png "http://in.linkedin.com/in/premaseem") Profile: http://in.linkedin.com/in/premaseem + +### Authors blog on design patterns: +https://premaseem.wordpress.com/category/computers/design-patterns/ + +### Software Design pattern community face book page: +https://www.facebook.com/DesignPatternGuru/ + +### Note: +* This code base will work on Java 9 and above versions. +* `diagrams` folders carry UML diagrams. +* `pattern` folder has code of primary example. +* `patternBonus` folder has code of secondary or bonus example. diff --git a/diagrams/State-Design-Pattern-class-diagram.jpeg b/diagrams/State-Design-Pattern-class-diagram.jpeg new file mode 100644 index 0000000..7e8f1bd Binary files /dev/null and b/diagrams/State-Design-Pattern-class-diagram.jpeg differ diff --git a/diagrams/State-Design-Pattern-generic.jpeg b/diagrams/State-Design-Pattern-generic.jpeg new file mode 100644 index 0000000..d9795ef Binary files /dev/null and b/diagrams/State-Design-Pattern-generic.jpeg differ diff --git a/diagrams/StatePatternSequenceDiagram.png b/diagrams/StatePatternSequenceDiagram.png new file mode 100644 index 0000000..d83fe76 Binary files /dev/null and b/diagrams/StatePatternSequenceDiagram.png differ diff --git a/pattern/src/com/premaseem/Client.java b/pattern/src/com/premaseem/Client.java deleted file mode 100644 index 15f05df..0000000 --- a/pattern/src/com/premaseem/Client.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.premaseem; - -/* -@author: Aseem Jain -@title: Design Patterns with Java 9 -@link: https://premaseem.wordpress.com/category/computers/design-patterns/ -@copyright: 2018 Packt Publication -*/ -public class Client { - public static void main (String[] args) { - System.out.println("Singleton cook example "); - } -} diff --git a/pattern/src/com/premaseem/statePattern/ClientForMedicalTreatment.java b/pattern/src/com/premaseem/statePattern/ClientForMedicalTreatment.java new file mode 100644 index 0000000..81b3fb1 --- /dev/null +++ b/pattern/src/com/premaseem/statePattern/ClientForMedicalTreatment.java @@ -0,0 +1,78 @@ +package com.premaseem.statePattern; + +/* +@author: Aseem Jain +@title: Design Patterns with Java 9 +@link: https://premaseem.wordpress.com/category/computers/design-patterns/ +*/ + +import java.util.Scanner; + +public class ClientForMedicalTreatment { + + public static void main (String[] args) { + Scanner scan = new Scanner(System.in); + String repeatRunFlag = "yes"; + + System.out.println("This is State Pattern example where in treatment would be dependent on the state of the patient ) "); + TreatmentContext context = new TreatmentContext(); + + while (repeatRunFlag.equalsIgnoreCase("yes")) { + // method takes patients state input + takePatientStateInput(context); + + System.out.println("Patient's State is - " + context.getState().getClass().getSimpleName() + ". Choose line of treatment ..."); + System.out.println(" Press 1 for admitting in ICU"); + System.out.println(" Press 2 for prescribing general Medicine "); + System.out.println(" Press 3 for supply of Oxygen"); + System.out.println(" Press 4 for supply of Normal food"); + System.out.println(" Press 5 for recommending a walk "); + + int treatmentType = scan.nextInt(); + + switch (treatmentType) { + case 1: + context.admitInICU(); + break; + case 2: + context.prescribeGeneralMedicine(); + break; + case 3: + context.supplyOxygen(); + break; + case 4: + context.supplyNormalFood(); + break; + + case 5: + context.recommendWalking(); + break; + } + + System.out.println(" ## State changed to: " + context.getState().getClass().getSimpleName()); + System.out.println("Press yes for further treatment and No to EXIT .... "); + try { + repeatRunFlag = scan.next(); + } catch (Exception e) { + repeatRunFlag = "No"; + } + } + } + + private static void takePatientStateInput (TreatmentContext context) { + Scanner scan = new Scanner(System.in); + System.out.println("Choose state of patient before starting treatment "); + System.out.println(" Press 1 for unstable state patient"); + System.out.println(" Press 2 for stable state patient "); + int entType = scan.nextInt(); + + switch (entType) { + case 1: + context.setState(context.unStablePatientState); + break; + case 2: + context.setState(context.stablePatientState); + break; + } + } +} diff --git a/pattern/src/com/premaseem/statePattern/PatientHealthState.java b/pattern/src/com/premaseem/statePattern/PatientHealthState.java new file mode 100644 index 0000000..61a1ff0 --- /dev/null +++ b/pattern/src/com/premaseem/statePattern/PatientHealthState.java @@ -0,0 +1,97 @@ +package com.premaseem.statePattern; + +/* +@author: Aseem Jain +@title: Design Patterns with Java 9 +@link: https://premaseem.wordpress.com/category/computers/design-patterns/ +*/ + +public interface PatientHealthState { + + void supplyOxygen (); + + void supplyOralFood (); + + void recommendWalking (); + + void prescribeGeneralMedicine (); + + void admitInICU (); +} + +class StablePatientState implements PatientHealthState { + + private TreatmentContext context; + + public StablePatientState (TreatmentContext context) { + this.context = context; + } + + @Override + public void supplyOxygen () { + System.out.println("Stable patient might become unstable if oxygen is forced fed. "); + context.setState(context.unStablePatientState); + } + + @Override + public void supplyOralFood () { + System.out.println(" Oral food will help faster recovery for Stable Patients. "); + context.setState(context.stablePatientState); + } + + @Override + public void recommendWalking () { + System.out.println(" Stable Patients should walk for about 1 mile each day. "); + context.setState(context.stablePatientState); + } + + @Override + public void prescribeGeneralMedicine () { + System.out.println(" will help in improvement "); + context.setState(context.stablePatientState); + } + + @Override + public void admitInICU () { + System.out.println(" Stable patient might become unstable if forced admit in ICU "); + context.setState(context.unStablePatientState); + } +} + +class UnStablePatientState implements PatientHealthState { + + private TreatmentContext context; + + public UnStablePatientState (TreatmentContext context) { + this.context = context; + } + + @Override + public void supplyOxygen () { + System.out.println("UnStable patient do need oxygen, will help in getting stable "); + context.setState(context.stablePatientState); + } + + @Override + public void supplyOralFood () { + System.out.println(" supply blood and liquids, will help in getting stable "); + context.setState(context.stablePatientState); + } + + @Override + public void recommendWalking () { + System.out.println("Not needed until patient becomes stable "); + context.setState(context.unStablePatientState); + } + + @Override + public void prescribeGeneralMedicine () { + System.out.println(" needs advance medicine "); + } + + @Override + public void admitInICU () { + System.out.println(" Intensive care is needed, will help in getting stable "); + context.setState(context.stablePatientState); + } +} diff --git a/pattern/src/com/premaseem/statePattern/PatternDescription.txt b/pattern/src/com/premaseem/statePattern/PatternDescription.txt new file mode 100644 index 0000000..2749e17 --- /dev/null +++ b/pattern/src/com/premaseem/statePattern/PatternDescription.txt @@ -0,0 +1,16 @@ +The state pattern, which closely resembles Strategy Pattern, is a behavioral software design pattern, also known as the objects for states pattern. This pattern is used in computer programming to encapsulate varying behavior for the same routine based on an object's state object. This can be a cleaner way for an object to change its behavior at runtime without resorting to large monolithic conditional statements. +In State pattern, we create objects which represent various states and a context object whose behavior varies as its state object changes. + +Problem solved by State pattern + +A monolithic object's behavior is a function of its state, and it must change its behavior at run-time depending on that state. Or, an application is characterixed by large and numerous case statements that vector flow of control based on the state of the application. + +Different between State and Strategy +Strategy Pattern is used when the whole algorithm is changed to another algorithm and the client is responsible for that, whereas, in State Pattern, the class itself manages the strategy based on the state. + +Two patterns are pretty similar in practice, and the defining difference between them tends to vary depending on who you ask. Some popular choices are: + +States store a reference to the context object that contains them. Strategies do not. +States are allowed to replace themselves (IE: to change the state of the context object to something else), while Strategies are not. +Strategies are passed to the context object as parameters, while States are created by the context object itself. +Strategies only handle a single, specific task, while States provide the underlying implementation for everything (or most everything) the context object does. \ No newline at end of file diff --git a/pattern/src/com/premaseem/statePattern/TreatmentContext.java b/pattern/src/com/premaseem/statePattern/TreatmentContext.java new file mode 100644 index 0000000..1caf848 --- /dev/null +++ b/pattern/src/com/premaseem/statePattern/TreatmentContext.java @@ -0,0 +1,45 @@ +package com.premaseem.statePattern; + +/* +@author: Aseem Jain +@title: Design Patterns with Java 9 +@link: https://premaseem.wordpress.com/category/computers/design-patterns/ +*/ + +public class TreatmentContext { + + PatientHealthState state; + StablePatientState stablePatientState = new StablePatientState(this); + UnStablePatientState unStablePatientState = new UnStablePatientState(this); + + + public PatientHealthState getState () { + return state; + } + + public void setState (PatientHealthState state) { + this.state = state; + } + + + void prescribeGeneralMedicine () { + state.prescribeGeneralMedicine(); + } + + void admitInICU () { + state.admitInICU(); + } + + void supplyOxygen () { + state.supplyOxygen(); + } + + void supplyNormalFood () { + state.supplyOralFood(); + } + + void recommendWalking () { + state.recommendWalking(); + } + +} diff --git a/patternBonus/src/com/premaseem/Client.java b/patternBonus/src/com/premaseem/Client.java deleted file mode 100644 index 15f05df..0000000 --- a/patternBonus/src/com/premaseem/Client.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.premaseem; - -/* -@author: Aseem Jain -@title: Design Patterns with Java 9 -@link: https://premaseem.wordpress.com/category/computers/design-patterns/ -@copyright: 2018 Packt Publication -*/ -public class Client { - public static void main (String[] args) { - System.out.println("Singleton cook example "); - } -}