解释器模式

  • 给定一个语言,定义它的文法的一种表示,并定义一个解释器,这个解释器使用该表示来解释语言中的句子。
    Interpreter
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
public interface Expression {
Object interpret(Context context);
}
//一种‘加’语言
public class Add implements Expression {
private Expression left;
private Expression right;
public Add(Expression left, Expression right) {
this.left = left;
this.right = right;
}
@Override
public Object interpret(Context context) {
return left.interpret(context).equals("CPU") && right.interpret(context).equals("Memory") ?
"主板" :
left.interpret(context).equals("Memory") && right.interpret(context).equals("CPU") ?
"主板" :
left.interpret(context).equals("主板") && right.interpret(context)
.equals("显示器") ? "电脑" : "四不像";
}
}
//一种‘减’语言
public class Sub implements Expression {
private Expression left;
private Expression right;
public Sub(Expression left, Expression right) {
this.left = left;
this.right = right;
}
@Override
public Object interpret(Context context) {
return left.interpret(context).equals("主板") && right.interpret(context).equals("CPU") ?
"Memory" :
right.interpret(context).equals("Memory") ? "CPU" : "四不像";
}
}
//一种‘连接’语言
public class Conn implements Expression {
private Expression left;
private Expression right;
public Conn(Expression left, Expression right) {
this.left = left;
this.right = right;
}
@Override
public Object interpret(Context context) {
return left.interpret(context).equals("电脑") && right.interpret(context).equals("电源") ?
"生产力" :
"废铁";
}
}
//一种‘终结表达式’语言
public class Variable implements Expression{
@Override
public Object interpret(Context context){
return context.LookupValue(this);
}
}
//一种‘终结表达式’语言
class Constant implements Expression {
private Object i;
public Constant(Object i) {
this.i = i;
}
@Override
public Object interpret(Context context) {
return i;
}
}
//一种‘缓存上下文的信息’语言,用于共享变量
public class Context {
private Map valueMap = new HashMap<>();
public void addValue(Variable x, Object y) {
valueMap.put(x, y);
}
public Object LookupValue(Variable x) {
return valueMap.get(x);
}
}
public class App {
public static void main(String[] args) {
// (((((CPU add Memory) sub CPU) and CPU) add 显示器) conn 电源)=生产力
Variable cpu = new Variable();
Variable mem = new Variable();
Variable ele = new Variable();
Constant screen = new Constant("显示器");
//Constant与Variable逻辑上的区别,本质应该一样,都是模式中的终结表达式对象,作为递归的终止条件
Context context = new Context();
context.addValue(cpu, "CPU");
context.addValue(mem, "Memory");
context.addValue(ele, "电源");
Expression expression = new Conn(
new Add(
new Add(
new Sub(
new Add(cpu, mem)
, cpu)
, cpu)
, screen)
, ele);
System.out.println(expression.interpret(context));//生产力
}
}

命令模式

  • 将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化;对请求排队或记录请求日志,以及支持可撤销的操作。
    Command
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
public class Light {
public void on() {
System.out.println("灯点亮");
}
public void off() {
System.out.println("灯熄灭");
}
}
public class TV {
public void on() {
System.out.println("TV打开");
}
public void off() {
System.out.println("TV关闭");
}
}
public interface Command {
void execute();
}
public class LightOnCommand implements Command{
private Light light;
public LightOnCommand(Light light) {
this.light = light;
}
@Override public void execute() {
light.on();
}
}
public class LightOffCommand implements Command {
private Light light;
public LightOffCommand(Light light) {
this.light = light;
}
@Override public void execute() {
light.off();
}
}
public class TVOnCommand implements Command{
private TV tv;
public TVOnCommand(TV tv) {
this.tv = tv;
}
@Override public void execute() {
tv.on();
}
}
public class TVOffCommand implements Command{
private TV tv;
public TVOffCommand(TV tv) {
this.tv = tv;
}
@Override public void execute() {
tv.off();
}
}
public class SwitchInvoker {
private Command onCommand;
private Command offCommand;
public void setOnCommand(Command onCommand) {
this.onCommand = onCommand;
}
public void setOffCommand(Command offCommand) {
this.offCommand = offCommand;
}
public void on(){
onCommand.execute();
}
public void off(){
offCommand.execute();
}
}
public class App {
public static void main(String[] args) {
//购买电器
Light light=new Light();
TV tv=new TV();
//配置电器
Command lightOnCommand=new LightOnCommand(light);
Command lightOffCommand=new LightOffCommand(light);
Command tvOnCommand=new TVOnCommand(tv);
Command tvOffCommand=new TVOffCommand(tv);
//安装开关并连接电器
SwitchInvoker commonSwitch=new SwitchInvoker();
//这一步相当于用电线将电器与开关相连接
//开关并不知道要开关谁,
commonSwitch.setOnCommand(lightOnCommand);
commonSwitch.setOffCommand(lightOffCommand);
//操作电器
commonSwitch.on();
commonSwitch.off();
//装修更改线路
//这里表达"使你可用不同的请求对客户参数化(将客户端参数化)"
commonSwitch.setOnCommand(tvOnCommand);
commonSwitch.setOffCommand(tvOffCommand);
//操作电器
commonSwitch.on();
commonSwitch.off();
}
}

责任链模式

  • 使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系。将这些对象连成一个链,沿着链条传递请求,知道有一个对象处理为止。

ChainOfResponsibility

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
public interface PhoneMakerAssembly {
void setNextPoint(PhoneMakerAssembly successor);
PhoneMakerAssembly getNextPoint();
void build(String partName);
}
public abstract class AbstractPhoneMakerAssembly implements PhoneMakerAssembly {
private PhoneMakerAssembly nextpoint;
@Override public void setNextPoint(PhoneMakerAssembly successor) {
nextpoint=successor;
}
@Override public PhoneMakerAssembly getNextPoint() {
return nextpoint;
}
}
public class Framemaker extends AbstractPhoneMakerAssembly{
@Override public void build(String partName) {
if ("main".equals(partName)){
System.out.println("构建主体框架");
return;
}
System.out.println("构建失败,寻找下家");
getNextPoint().build(partName);
}
}
public class CameraMaker extends AbstractPhoneMakerAssembly{
@Override public void build(String partName) {
if ("camera".equals(partName)){
System.out.println("构建摄像头框架");
return;
}
System.out.println("构建失败,寻找下家");
getNextPoint().build(partName);
}
}
public class ScreenMaker extends AbstractPhoneMakerAssembly{
@Override public void build(String partName) {
if ("screen".equals(partName)){
System.out.println("构建屏幕框架");
return;
}
System.out.println("构建失败,寻找下家");
getNextPoint().build(partName);
}
}
public class DefaultMaker implements PhoneMakerAssembly {
@Override public void setNextPoint(PhoneMakerAssembly successor) {
}
@Override public PhoneMakerAssembly getNextPoint() {
return null;
}
@Override public void build(String partName) {
System.out.println("未找到支持的构建器");
}
}
public class App {
public static void main(String[] args) {
PhoneMakerAssembly maker1=new Framemaker();
PhoneMakerAssembly maker2=new CameraMaker();
PhoneMakerAssembly maker3=new ScreenMaker();
maker1.setNextPoint(maker2);
maker2.setNextPoint(maker3);
maker3.setNextPoint(new DefaultMaker());
//注意链条是代码构造出来的
maker1.build("screen");
maker1.build("main");
maker1.build("camera");
maker1.build("xxx");
}
}