forked from BruceEckel/OnJava8-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApplicator.java
More file actions
64 lines (57 loc) · 1.78 KB
/
Copy pathApplicator.java
File metadata and controls
64 lines (57 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// interfaces/Applicator.java
// (c)2020 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
import java.util.*;
class Processor {
public String name() {
return getClass().getSimpleName();
}
public Object process(Object input) {
return input;
}
}
class Upcase extends Processor {
@Override // Covariant return:
public String process(Object input) {
return ((String)input).toUpperCase();
}
}
class Downcase extends Processor {
@Override
public String process(Object input) {
return ((String)input).toLowerCase();
}
}
class Splitter extends Processor {
@Override
public String process(Object input) {
// split() divides a String into pieces:
return Arrays.toString(((String)input).split(" "));
}
}
public class Applicator {
public static void apply(Processor p, Object s) {
System.err.println("Using Processor " + p.name());
System.err.println(p.process(s));
}
public static void main(String[] args) {
String s =
"We are such stuff as dreams are made on";
apply(new Upcase(), s);
apply(new Downcase(), s);
apply(new Splitter(), s);
}
}
/* Output:
Using Processor Upcase
WE ARE SUCH STUFF AS DREAMS ARE MADE ON
Using Processor Downcase
we are such stuff as dreams are made on
Using Processor Splitter
[We, are, such, stuff, as, dreams, are, made, on]
self-note: 像本例中这样,创建一个能根据传入的参数类型从而具备不同行为的方法称为策略设计模式。
方法包含算法中不变的部分,策略包含变化的部分。
策略就是传入的对象,它包含要执行的代码。
在这里,Processor 对象是策略,main() 方法展示了三种不同的应用于 String s 上的策略。
*/