From 68fc4fdf1d7b085878353aea0e180c082ecf4735 Mon Sep 17 00:00:00 2001 From: rainbow Date: Mon, 21 Sep 2020 10:53:02 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D25=E7=AB=A0-=E6=A8=A1?= =?UTF-8?q?=E6=9D=BF=E6=96=B9=E6=B3=95=E6=A8=A1=E5=BC=8F=E4=B8=AD=E7=9A=84?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=A0=BC=E5=BC=8F=E9=97=AE=E9=A2=98=20(#590)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 修改25章-模板方法模式中的代码格式问题 * fix more code format error --- docs/book/25-Patterns.md | 58 +++++++++++++++++++++++++++++++--------- 1 file changed, 45 insertions(+), 13 deletions(-) diff --git a/docs/book/25-Patterns.md b/docs/book/25-Patterns.md index f3d3764..43c4f92 100644 --- a/docs/book/25-Patterns.md +++ b/docs/book/25-Patterns.md @@ -122,8 +122,26 @@ abstract class ApplicationFramework { abstract void customize1(); - abstract void customize2(); // "private" means automatically "final": private void templateMethod() { IntStream.range(0, 5).forEach( n -> { customize1(); customize2(); }); }}// Create a new "application": class MyApp extends ApplicationFramework { @Override void customize1() { System.out.print("Hello "); }@Override + abstract void customize2(); + // "private" means automatically "final": + private void templateMethod() { + IntStream.range(0, 5).forEach( + n -> { + customize1(); + customize2(); + }); + } +} + +// Create a new "application": +class MyApp extends ApplicationFramework { + @Override + void customize1() { + System.out.print("Hello "); + } + + @Override void customize2() { System.out.println("World!"); } @@ -134,8 +152,7 @@ public class TemplateMethod { new MyApp(); } } -/* -Output: +/* Output: Hello World! Hello World! Hello World! @@ -245,8 +262,20 @@ class State implements StateBase { @Override public void changeImp(StateBase newImp) { implementation = newImp; - }// Pass method calls to the implementation: @Override public void f() { implementation.f(); } @Override public void g() { implementation.g(); } @Override + } + // Pass method calls to the implementation: + @Override + public void f() { + implementation.f(); + } + + @Override + public void g() { + implementation.g(); + } + + @Override public void h() { implementation.h(); } @@ -302,7 +331,8 @@ public class StateDemo { } public static void main(String[] args) { - StateBase b = new State(new Implementation1()); + StateBase b = + new State(new Implementation1()); test(b); b.changeImp(new Implementation2()); test(b); @@ -349,9 +379,6 @@ interface State { abstract class StateMachine { protected State currentState; - Nap(0.5); -System.out.println("Washing"); new - protected abstract boolean changeState(); // Template method: @@ -362,9 +389,12 @@ System.out.println("Washing"); new } // A different subclass for each state: + class Wash implements State { @Override public void run() { + System.out.println("Washing"); + new Nap(0.5); } } @@ -386,9 +416,11 @@ class Rinse implements State { class Washer extends StateMachine { private int i = 0; - // The state table: - private State[] states = {new Wash(), new Spin(), new Rinse(), new Spin(),}; + private State[] states = { + new Wash(), new Spin(), + new Rinse(), new Spin(), + }; Washer() { runAll(); @@ -401,7 +433,8 @@ class Washer extends StateMachine { // surrogate reference to a new object: currentState = states[i++]; return true; - } else return false; + } else + return false; } } @@ -410,8 +443,7 @@ public class StateMachineDemo { new Washer(); } } -/* -Output: +/* Output: Washing Spinning Rinsing