Skip to content

Commit fc6e5e5

Browse files
design pattern: strategy pattern
1 parent 55b8d0b commit fc6e5e5

File tree

9 files changed

+111
-0
lines changed

9 files changed

+111
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package learnHeadFirstDesignPatterns.chapter_1_strategy_pattern;
2+
3+
public interface CookBehavior {
4+
5+
public void cook();
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package learnHeadFirstDesignPatterns.chapter_1_strategy_pattern;
2+
3+
public class CookNoodlesBehavior implements CookBehavior {
4+
5+
@Override
6+
public void cook() {
7+
System.out.println("I'm also fond of cooking Chinese noodles!");
8+
}
9+
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package learnHeadFirstDesignPatterns.chapter_1_strategy_pattern;
2+
3+
public class CookRiceFlour implements CookBehavior {
4+
5+
@Override
6+
public void cook() {
7+
System.out.println("I'm cooking MiFen, aka, Rice Flour.");
8+
}
9+
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package learnHeadFirstDesignPatterns.chapter_1_strategy_pattern;
2+
3+
public interface LoveBehavior {
4+
5+
public void showLove();
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package learnHeadFirstDesignPatterns.chapter_1_strategy_pattern;
2+
3+
public class LoveHusbandBehavior implements LoveBehavior {
4+
5+
@Override
6+
public void showLove() {
7+
System.out.println("I love my husband Steve Sun, he is just awesome!");
8+
}
9+
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package learnHeadFirstDesignPatterns.chapter_1_strategy_pattern;
2+
3+
public class ModelWife extends Wife {
4+
5+
public ModelWife() {
6+
loveBehavior = new LoveHusbandBehavior();
7+
cookBehavior = new CookRiceFlour();
8+
}
9+
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package learnHeadFirstDesignPatterns.chapter_1_strategy_pattern;
2+
3+
public class RealWife extends Wife {
4+
5+
public RealWife() {
6+
System.out.println("I'm the real wife - Sophie Yan! I'm Sophie Yan, the wife of Steve Sun, and I like cooking for my husband and family. ");
7+
this.cookBehavior = new CookRiceFlour();
8+
this.loveBehavior = new LoveHusbandBehavior();
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package learnHeadFirstDesignPatterns.chapter_1_strategy_pattern;
2+
3+
/**
4+
* The Strategy Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable.
5+
* Strategy lets the algorithm vary independently from clients that use it.*/
6+
7+
/**Design principle:
8+
* Favor composition over inheritance.
9+
*
10+
* Not only does it let you encapsulate a family of algorithms into their own set of classes, but it also lets you change behavior at runtime.*/
11+
public class StrategtyPatternTestDrive {
12+
13+
public static void main(String[] args) {
14+
Wife wife = new RealWife();
15+
wife.performLove();
16+
wife.performCook();;
17+
18+
wife.setCookBehavior(new CookNoodlesBehavior());
19+
wife.performCook();
20+
}
21+
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package learnHeadFirstDesignPatterns.chapter_1_strategy_pattern;
2+
3+
public abstract class Wife {
4+
5+
CookBehavior cookBehavior;
6+
LoveBehavior loveBehavior;
7+
8+
public Wife () {
9+
System.out.println("I'm the abstract wife - Sophie Yan!");
10+
};
11+
12+
public void setCookBehavior(CookBehavior cb){
13+
cookBehavior = cb;
14+
}
15+
16+
public void setLoveBehavior(LoveBehavior lb){
17+
loveBehavior = lb;
18+
}
19+
20+
public void performCook(){
21+
cookBehavior.cook();
22+
}
23+
24+
public void performLove(){
25+
loveBehavior.showLove();
26+
}
27+
}

0 commit comments

Comments
 (0)