File tree Expand file tree Collapse file tree
HeadFirstDesignPatterns/src Expand file tree Collapse file tree Original file line number Diff line number Diff line change 77
88一些常見的還有像把Map 轉成Json Object也很合適去製作一個Adapter。
99____
10+ ##樣板方法模式
11+ ____
12+ 樣板方法模式:將一個演算法的骨架定義在一個方法中,而演算法本身會用的一些方法,則是定義在次類別中。樣板方法讓次類別可以在不改變演算法架構的情況下,重新定義演算法中的某個步驟。
13+
14+ 跟工廠模式相當類似,一個是把類別實作的取得交給次類別去決定,一個是演算法包在父類別,而讓子類別可以決定方法的實作,有兩種方式,一種是使用抽象方法,另一種是先把一個方法放在父類別而做預設的事,子類別可以決定要不要複寫方法(掛鉤)。
15+ ____
Original file line number Diff line number Diff line change 11package decorate ;
22
3- public abstract class Enchanting implements Weapon {
3+ import prototype .ThrowingObjects ;
4+
5+ public abstract class Enchanting implements Weapon , ThrowingObjects {
46 protected Weapon mWeapon ;
7+ protected ThrowingObjects mThrowingObjects ;
58 public Enchanting (Weapon weapon ) {
69 this .mWeapon = weapon ;
710 }
11+ public Enchanting (ThrowingObjects throwingobjects ){
12+ this .mThrowingObjects = throwingobjects ;
13+ }
814}
Original file line number Diff line number Diff line change 11package decorate ;
22
3+ import prototype .ThrowingObjects ;
4+
35public class Fire extends Enchanting {
46
57 public Fire (Weapon weapon ) {
68 super (weapon );
79 }
10+ public Fire (ThrowingObjects throwingobjects ){
11+ super (throwingobjects );
12+ }
13+
814
915 @ Override
1016 public int makeDamage () {
@@ -15,5 +21,11 @@ public int makeDamage() {
1521 public String getHurtVerb () {
1622 return "¨`¼öªº" + mWeapon .getHurtVerb ();
1723 }
24+
25+ @ Override
26+ public String ThrowOutWord () {
27+ return "¨`¼öªº" + mThrowingObjects .ThrowOutWord ();
28+ }
29+
1830
1931}
Original file line number Diff line number Diff line change 11package decorate ;
22
3+ import prototype .ThrowingObjects ;
4+
35public class Ice extends Enchanting {
46
57 public Ice (Weapon weapon ) {
68 super (weapon );
79 }
10+ public Ice (ThrowingObjects throwingobjects ){
11+ super (throwingobjects );
12+ }
813
914 @ Override
1015 public int makeDamage () {
@@ -16,4 +21,9 @@ public String getHurtVerb() {
1621 return "´H§Nªº" + mWeapon .getHurtVerb ();
1722 }
1823
24+ @ Override
25+ public String ThrowOutWord () {
26+ return "´H§Nªº" + mThrowingObjects .ThrowOutWord ();
27+ }
28+
1929}
Original file line number Diff line number Diff line change 1+ package prototype ;
2+
3+ import util .JavaLog ;
4+
5+ public class Ballista extends Thrower {
6+
7+
8+ public Ballista () {
9+ super (new GiantCrossbow ());
10+ }
11+
12+ // 由這個類別決定要做什麼,這邊要做的是準備好弓弩,準備的使用在父類別。
13+ @ Override
14+ protected void setupThrowingObject () {
15+ setupCrossbow ();
16+ }
17+
18+ private void setupCrossbow (){
19+ JavaLog .d ("將巨型弩箭安裝至定位" );
20+ }
21+
22+ // 取消火附魔
23+ @ Override
24+ public boolean hasfire () {
25+ return false ;
26+ }
27+
28+ }
Original file line number Diff line number Diff line change 1+ package prototype ;
2+
3+ import util .JavaLog ;
4+
5+ public class Catapults extends Thrower {
6+
7+ public Catapults () {
8+ super (new Rock ());
9+ }
10+
11+ @ Override
12+ protected void setupThrowingObject () {
13+ setupRockToThrower ();
14+ }
15+
16+ private void setupRockToThrower (){
17+ JavaLog .d ("§â¥ÛÀY¦w¸Ë¦n¡C" );
18+ }
19+
20+
21+
22+
23+ }
Original file line number Diff line number Diff line change 1+ package prototype ;
2+
3+ public class GiantCrossbow implements ThrowingObjects {
4+ @ Override
5+ public String ThrowOutWord () {
6+ return "µo®g¥X¤F¥¨«¬©¸½b" ;
7+ }
8+ }
Original file line number Diff line number Diff line change 1+ package prototype ;
2+
3+ public class Go {
4+
5+ public static void main (String [] args ) {
6+ Thrower t1 = new Ballista ();
7+ Thrower t2 = new Catapults ();
8+ t1 .prepare ();
9+ t2 .prepare ();
10+ t1 .Throw ();
11+ t2 .Throw ();
12+
13+ }
14+ }
Original file line number Diff line number Diff line change 1+ ##樣板方法模式
2+ ____
3+ 樣板方法模式:將一個演算法的骨架定義在一個方法中,而演算法本身會用的一些方法,則是定義在次類別中。樣板方法讓次類別可以在不改變演算法架構的情況下,重新定義演算法中的某個步驟。
4+
5+ 跟工廠模式相當類似,一個是把類別實作的取得交給次類別去決定,一個是演算法包在父類別,而讓子類別可以決定方法的實作,有兩種方式,一種是使用抽象方法,另一種是先把一個方法放在父類別而做預設的事,子類別可以決定要不要複寫方法(掛鉤)。
6+ ____
Original file line number Diff line number Diff line change 1+ package prototype ;
2+
3+ public class Rock implements ThrowingObjects {
4+ @ Override
5+ public String ThrowOutWord () {
6+ return "©ß¥X¤F¤@Áû¥ÛÀY§r" ;
7+ }
8+ }
You can’t perform that action at this time.
0 commit comments