mirror of
https://github.com/LingCoder/OnJava8.git
synced 2026-08-19 12:33:29 +08:00
修复25章-模板方法模式中的代码格式问题 (#590)
* 修改25章-模板方法模式中的代码格式问题 * fix more code format error
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user