From 14dff017508d679ed929d588160eb8d84c436d72 Mon Sep 17 00:00:00 2001 From: RajkinHossain Date: Mon, 22 Aug 2022 22:21:33 +0600 Subject: [PATCH] Update Strategy patten source code --- java/strategy/.classpath | 6 ++++++ java/strategy/.gitignore | 13 +++++++++++++ java/strategy/.project | 17 +++++++++++++++++ java/strategy/DuckSimulator.java | 25 +++++++++++++++++++++++++ java/strategy/FlyNoWay.java | 5 ----- java/strategy/MallardDuck.java | 6 ++++-- java/strategy/MiniDuckSimulator.java | 12 ------------ java/strategy/ModelDuck.java | 6 ++++-- 8 files changed, 69 insertions(+), 21 deletions(-) create mode 100644 java/strategy/.classpath create mode 100644 java/strategy/.gitignore create mode 100644 java/strategy/.project create mode 100644 java/strategy/DuckSimulator.java delete mode 100644 java/strategy/FlyNoWay.java delete mode 100644 java/strategy/MiniDuckSimulator.java diff --git a/java/strategy/.classpath b/java/strategy/.classpath new file mode 100644 index 0000000..3f3893a --- /dev/null +++ b/java/strategy/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/java/strategy/.gitignore b/java/strategy/.gitignore new file mode 100644 index 0000000..2915fe6 --- /dev/null +++ b/java/strategy/.gitignore @@ -0,0 +1,13 @@ +/Duck.class +/FlyBehavior.class +/FlyNoWay.class +/FlyRocketPowered.class +/FlyWithWings.class +/MallardDuck.class +/MiniDuckSimulator.class +/ModelDuck.class +/MuteQuack.class +/Quack.class +/QuackBehavior.class +/Squeak.class +/DuckSimulator.class diff --git a/java/strategy/.project b/java/strategy/.project new file mode 100644 index 0000000..20a383a --- /dev/null +++ b/java/strategy/.project @@ -0,0 +1,17 @@ + + + strategy + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/java/strategy/DuckSimulator.java b/java/strategy/DuckSimulator.java new file mode 100644 index 0000000..38cd1fc --- /dev/null +++ b/java/strategy/DuckSimulator.java @@ -0,0 +1,25 @@ +public class DuckSimulator { + public static void main(String[] args) { + Duck mallard = new MallardDuck(); // A duck object + + mallard.setFlyBehavior(new FlyWithWings()); // Set an Algorithm(Fly): FlyWithWings + mallard.performFly(); // Fly based on Algorithm FlyWithWings + + // So I can change the algorithm in future with the help of Set method where I do not need to change existing code + // and inject the updated or new algorithm + + mallard.setQuackBehavior(new Squeak()); // Set an Algorithm(Quack): Squeak + mallard.performQuack(); // Quack based on Algorithm Squeak + + Duck model = new ModelDuck(); // Another duck object + model.setFlyBehavior(new FlyRocketPowered()); //Set a Algorithm(Fly): FlyRocketPowered + model.performFly(); // Fly based on Algorithm FlyRocketPowered + + // Suppose this kind of duck can not quack so create an algorithm (no quack ability) and inject it + model.setQuackBehavior(new MuteQuack()); + model.performQuack(); + } + + //Note: We can also set the algorithms inside the duck object Class: MallardDuck or ModelDuck + //But we may need to change the algorithm in future which violates open close principle +} \ No newline at end of file diff --git a/java/strategy/FlyNoWay.java b/java/strategy/FlyNoWay.java deleted file mode 100644 index 209d27b..0000000 --- a/java/strategy/FlyNoWay.java +++ /dev/null @@ -1,5 +0,0 @@ -public class FlyNoWay implements FlyBehavior { - public void fly() { - System.out.println("I can't fly."); - } -} \ No newline at end of file diff --git a/java/strategy/MallardDuck.java b/java/strategy/MallardDuck.java index aa71af5..215ce24 100644 --- a/java/strategy/MallardDuck.java +++ b/java/strategy/MallardDuck.java @@ -1,7 +1,9 @@ public class MallardDuck extends Duck { public MallardDuck() { - quackBehavior = new Quack(); - flyBehavior = new FlyWithWings(); + //We also can set manually the algorithms from the class itself: like below code + + //quackBehavior = new Quack(); + //flyBehavior = new FlyWithWings(); } public void display() { diff --git a/java/strategy/MiniDuckSimulator.java b/java/strategy/MiniDuckSimulator.java deleted file mode 100644 index 3c8e9d1..0000000 --- a/java/strategy/MiniDuckSimulator.java +++ /dev/null @@ -1,12 +0,0 @@ -public class MiniDuckSimulator { - public static void main(String[] args) { - Duck mallard = new MallardDuck(); - mallard.performQuack(); // Quack - mallard.performFly(); // I'm flying!! - - Duck model = new ModelDuck(); - model.performFly(); // I can't fly - model.setFlyBehavior(new FlyRocketPowered()); - model.performFly(); // I'm flying with a rocket - } -} \ No newline at end of file diff --git a/java/strategy/ModelDuck.java b/java/strategy/ModelDuck.java index 7505911..3d43edd 100644 --- a/java/strategy/ModelDuck.java +++ b/java/strategy/ModelDuck.java @@ -1,7 +1,9 @@ public class ModelDuck extends Duck { public ModelDuck(){ - flyBehavior = new FlyNoWay(); - quackBehavior = new Quack(); + //We also can set manually the algorithms from the class itself: like below code + + //quackBehavior = new Quack(); + //flyBehavior = new FlyWithWings(); } public void display() {