Skip to content

Update Strategy patten source code #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions java/strategy/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="src" path=""/>
<classpathentry kind="output" path=""/>
</classpath>
13 changes: 13 additions & 0 deletions java/strategy/.gitignore
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions java/strategy/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>strategy</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
25 changes: 25 additions & 0 deletions java/strategy/DuckSimulator.java
Original file line number Diff line number Diff line change
@@ -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
}
5 changes: 0 additions & 5 deletions java/strategy/FlyNoWay.java

This file was deleted.

6 changes: 4 additions & 2 deletions java/strategy/MallardDuck.java
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down
12 changes: 0 additions & 12 deletions java/strategy/MiniDuckSimulator.java

This file was deleted.

6 changes: 4 additions & 2 deletions java/strategy/ModelDuck.java
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down