Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitfc6e5e5

Browse files
design pattern: strategy pattern
1 parent55b8d0b commitfc6e5e5

File tree

9 files changed

+111
-0
lines changed

9 files changed

+111
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
packagelearnHeadFirstDesignPatterns.chapter_1_strategy_pattern;
2+
3+
publicinterfaceCookBehavior {
4+
5+
publicvoidcook();
6+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
packagelearnHeadFirstDesignPatterns.chapter_1_strategy_pattern;
2+
3+
publicclassCookNoodlesBehaviorimplementsCookBehavior {
4+
5+
@Override
6+
publicvoidcook() {
7+
System.out.println("I'm also fond of cooking Chinese noodles!");
8+
}
9+
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
packagelearnHeadFirstDesignPatterns.chapter_1_strategy_pattern;
2+
3+
publicclassCookRiceFlourimplementsCookBehavior {
4+
5+
@Override
6+
publicvoidcook() {
7+
System.out.println("I'm cooking MiFen, aka, Rice Flour.");
8+
}
9+
10+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
packagelearnHeadFirstDesignPatterns.chapter_1_strategy_pattern;
2+
3+
publicinterfaceLoveBehavior {
4+
5+
publicvoidshowLove();
6+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
packagelearnHeadFirstDesignPatterns.chapter_1_strategy_pattern;
2+
3+
publicclassLoveHusbandBehaviorimplementsLoveBehavior {
4+
5+
@Override
6+
publicvoidshowLove() {
7+
System.out.println("I love my husband Steve Sun, he is just awesome!");
8+
}
9+
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
packagelearnHeadFirstDesignPatterns.chapter_1_strategy_pattern;
2+
3+
publicclassModelWifeextendsWife {
4+
5+
publicModelWife() {
6+
loveBehavior =newLoveHusbandBehavior();
7+
cookBehavior =newCookRiceFlour();
8+
}
9+
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
packagelearnHeadFirstDesignPatterns.chapter_1_strategy_pattern;
2+
3+
publicclassRealWifeextendsWife {
4+
5+
publicRealWife() {
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 =newCookRiceFlour();
8+
this.loveBehavior =newLoveHusbandBehavior();
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
packagelearnHeadFirstDesignPatterns.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+
publicclassStrategtyPatternTestDrive {
12+
13+
publicstaticvoidmain(String[]args) {
14+
Wifewife =newRealWife();
15+
wife.performLove();
16+
wife.performCook();;
17+
18+
wife.setCookBehavior(newCookNoodlesBehavior());
19+
wife.performCook();
20+
}
21+
22+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
packagelearnHeadFirstDesignPatterns.chapter_1_strategy_pattern;
2+
3+
publicabstractclassWife {
4+
5+
CookBehaviorcookBehavior;
6+
LoveBehaviorloveBehavior;
7+
8+
publicWife () {
9+
System.out.println("I'm the abstract wife - Sophie Yan!");
10+
};
11+
12+
publicvoidsetCookBehavior(CookBehaviorcb){
13+
cookBehavior =cb;
14+
}
15+
16+
publicvoidsetLoveBehavior(LoveBehaviorlb){
17+
loveBehavior =lb;
18+
}
19+
20+
publicvoidperformCook(){
21+
cookBehavior.cook();
22+
}
23+
24+
publicvoidperformLove(){
25+
loveBehavior.showLove();
26+
}
27+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp