Skip to content

Commit e612748

Browse files
committed
State Design Pattern - Medical Treatment application
1 parent 6deb6b9 commit e612748

File tree

6 files changed

+236
-26
lines changed

6 files changed

+236
-26
lines changed

pattern/src/com/premaseem/Client.java

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.premaseem.statePattern;
2+
3+
/*
4+
@author: Aseem Jain
5+
@title: Design Patterns with Java 9
6+
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
7+
*/
8+
9+
import java.util.Scanner;
10+
11+
public class ClientForMedicalTreatment {
12+
13+
public static void main (String[] args) {
14+
Scanner scan = new Scanner(System.in);
15+
String repeatRunFlag = "yes";
16+
17+
System.out.println("This is State Pattern example where in treatment would be dependent on the state of the patient ) ");
18+
TreatmentContext context = new TreatmentContext();
19+
20+
while (repeatRunFlag.equalsIgnoreCase("yes")) {
21+
// method takes patients state input
22+
takePatientStateInput(context);
23+
24+
System.out.println("Patient's State is - " + context.getState().getClass().getSimpleName() + ". Choose line of treatment ...");
25+
System.out.println(" Press 1 for admitting in ICU");
26+
System.out.println(" Press 2 for prescribing general Medicine ");
27+
System.out.println(" Press 3 for supply of Oxygen");
28+
System.out.println(" Press 4 for supply of Normal food");
29+
System.out.println(" Press 5 for recommending a walk ");
30+
31+
int treatmentType = scan.nextInt();
32+
33+
switch (treatmentType) {
34+
case 1:
35+
context.admitInICU();
36+
break;
37+
case 2:
38+
context.prescribeGeneralMedicine();
39+
break;
40+
case 3:
41+
context.supplyOxygen();
42+
break;
43+
case 4:
44+
context.supplyNormalFood();
45+
break;
46+
47+
case 5:
48+
context.recommendWalking();
49+
break;
50+
}
51+
52+
System.out.println(" ## State changed to: " + context.getState().getClass().getSimpleName());
53+
System.out.println("Press yes for further treatment and No to EXIT .... ");
54+
try {
55+
repeatRunFlag = scan.next();
56+
} catch (Exception e) {
57+
repeatRunFlag = "No";
58+
}
59+
}
60+
}
61+
62+
private static void takePatientStateInput (TreatmentContext context) {
63+
Scanner scan = new Scanner(System.in);
64+
System.out.println("Choose state of patient before starting treatment ");
65+
System.out.println(" Press 1 for unstable state patient");
66+
System.out.println(" Press 2 for stable state patient ");
67+
int entType = scan.nextInt();
68+
69+
switch (entType) {
70+
case 1:
71+
context.setState(context.unStablePatientState);
72+
break;
73+
case 2:
74+
context.setState(context.stablePatientState);
75+
break;
76+
}
77+
}
78+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package com.premaseem.statePattern;
2+
3+
/*
4+
@author: Aseem Jain
5+
@title: Design Patterns with Java 9
6+
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
7+
*/
8+
9+
public interface PatientHealthState {
10+
11+
void supplyOxygen ();
12+
13+
void supplyOralFood ();
14+
15+
void recommendWalking ();
16+
17+
void prescribeGeneralMedicine ();
18+
19+
void admitInICU ();
20+
}
21+
22+
class StablePatientState implements PatientHealthState {
23+
24+
private TreatmentContext context;
25+
26+
public StablePatientState (TreatmentContext context) {
27+
this.context = context;
28+
}
29+
30+
@Override
31+
public void supplyOxygen () {
32+
System.out.println("Stable patient might become unstable if oxygen is forced fed. ");
33+
context.setState(context.unStablePatientState);
34+
}
35+
36+
@Override
37+
public void supplyOralFood () {
38+
System.out.println(" Oral food will help faster recovery for Stable Patients. ");
39+
context.setState(context.stablePatientState);
40+
}
41+
42+
@Override
43+
public void recommendWalking () {
44+
System.out.println(" Stable Patients should walk for about 1 mile each day. ");
45+
context.setState(context.stablePatientState);
46+
}
47+
48+
@Override
49+
public void prescribeGeneralMedicine () {
50+
System.out.println(" will help in improvement ");
51+
context.setState(context.stablePatientState);
52+
}
53+
54+
@Override
55+
public void admitInICU () {
56+
System.out.println(" Stable patient might become unstable if forced admit in ICU ");
57+
context.setState(context.unStablePatientState);
58+
}
59+
}
60+
61+
class UnStablePatientState implements PatientHealthState {
62+
63+
private TreatmentContext context;
64+
65+
public UnStablePatientState (TreatmentContext context) {
66+
this.context = context;
67+
}
68+
69+
@Override
70+
public void supplyOxygen () {
71+
System.out.println("UnStable patient do need oxygen, will help in getting stable ");
72+
context.setState(context.stablePatientState);
73+
}
74+
75+
@Override
76+
public void supplyOralFood () {
77+
System.out.println(" supply blood and liquids, will help in getting stable ");
78+
context.setState(context.stablePatientState);
79+
}
80+
81+
@Override
82+
public void recommendWalking () {
83+
System.out.println("Not needed until patient becomes stable ");
84+
context.setState(context.unStablePatientState);
85+
}
86+
87+
@Override
88+
public void prescribeGeneralMedicine () {
89+
System.out.println(" needs advance medicine ");
90+
}
91+
92+
@Override
93+
public void admitInICU () {
94+
System.out.println(" Intensive care is needed, will help in getting stable ");
95+
context.setState(context.stablePatientState);
96+
}
97+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
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.
2+
In State pattern, we create objects which represent various states and a context object whose behavior varies as its state object changes.
3+
4+
Problem solved by State pattern
5+
6+
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.
7+
8+
Different between State and Strategy
9+
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.
10+
11+
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:
12+
13+
States store a reference to the context object that contains them. Strategies do not.
14+
States are allowed to replace themselves (IE: to change the state of the context object to something else), while Strategies are not.
15+
Strategies are passed to the context object as parameters, while States are created by the context object itself.
16+
Strategies only handle a single, specific task, while States provide the underlying implementation for everything (or most everything) the context object does.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.premaseem.statePattern;
2+
3+
/*
4+
@author: Aseem Jain
5+
@title: Design Patterns with Java 9
6+
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
7+
*/
8+
9+
public class TreatmentContext {
10+
11+
PatientHealthState state;
12+
StablePatientState stablePatientState = new StablePatientState(this);
13+
UnStablePatientState unStablePatientState = new UnStablePatientState(this);
14+
15+
16+
public PatientHealthState getState () {
17+
return state;
18+
}
19+
20+
public void setState (PatientHealthState state) {
21+
this.state = state;
22+
}
23+
24+
25+
void prescribeGeneralMedicine () {
26+
state.prescribeGeneralMedicine();
27+
}
28+
29+
void admitInICU () {
30+
state.admitInICU();
31+
}
32+
33+
void supplyOxygen () {
34+
state.supplyOxygen();
35+
}
36+
37+
void supplyNormalFood () {
38+
state.supplyOralFood();
39+
}
40+
41+
void recommendWalking () {
42+
state.recommendWalking();
43+
}
44+
45+
}

patternBonus/src/com/premaseem/Client.java

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)