From 20370c0ede4ca0aa5bd2073e23648b26af5ff763 Mon Sep 17 00:00:00 2001 From: shlinym Date: Sat, 19 Sep 2020 15:31:54 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E6=94=B925=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?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/book/25-Patterns.md | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/docs/book/25-Patterns.md b/docs/book/25-Patterns.md index f3d37647..7bba907f 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! From 61022d637cbcf690fa03f2393d7895909fe2594c Mon Sep 17 00:00:00 2001 From: shlinym Date: Sat, 19 Sep 2020 16:44:23 +0800 Subject: [PATCH 2/2] fix more code format error --- docs/book/25-Patterns.md | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/docs/book/25-Patterns.md b/docs/book/25-Patterns.md index 7bba907f..43c4f929 100644 --- a/docs/book/25-Patterns.md +++ b/docs/book/25-Patterns.md @@ -262,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(); } @@ -319,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); @@ -366,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: @@ -379,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); } } @@ -403,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(); @@ -418,7 +433,8 @@ class Washer extends StateMachine { // surrogate reference to a new object: currentState = states[i++]; return true; - } else return false; + } else + return false; } } @@ -427,8 +443,7 @@ public class StateMachineDemo { new Washer(); } } -/* -Output: +/* Output: Washing Spinning Rinsing