In Strategy pattern, a class behavior or its algorithm can be changed at run time. This type of design pattern comes under behavior pattern. In this we create objects which represent various strategies and a context object whose behavior varies as per its strategy object. The strategy object changes the executing algorithm of the context object.
Strategy Design Pattern for Hospital Management System |
Strategy.java
package SMD5;
public interface Strategy {
public void illness();
}
Patient.java
package SMD5; public class Patient { private Strategy strategy; public Patient(Strategy strategy){ this.strategy = strategy; } public void executeStrategy(){ strategy.illness(); } }Cured.java
package SMD5; public class Cured implements Strategy{ public void illness() { System.out.println("Im cured and im paying Bill Bill amount: 5000"); } }NotCured.java
package SMD5; public class NotCured implements Strategy{ public void illness() { System.out.println("Im not cured and going under diagnose again"); } }StrategyMain.java
package SMD5; public class StrategyMain { public static void main(String[] args) { System.out.println("Im under diagnosed"); System.out.println("After Diagnose"); Patient P = new Patient(new Cured()); P.executeStrategy(); System.out.println("After Diagnose"); Patient P1 = new Patient(new NotCured()); P1.executeStrategy(); } }
Comments
Post a Comment