文章目录
  1. 外观模式

外观模式

  • 为子系统中的一组接口提供一个一致的界面,定义了一个高层接口使得子系统更加容易使用

Facade

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
public interface IMoudle {
String getName();
void add(IMoudle moudle);
void remove(IMoudle moudle);
}
public class Moudle implements IMoudle {
private String name;
private List<IMoudle> childrenMoudleList;
public Moudle(int price, String name) {
this.name = name;
childrenMoudleList = new ArrayList<>();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void add(IMoudle moudle) {
childrenMoudleList.add(moudle);
}
@Override
public void remove(IMoudle moudle) {
childrenMoudleList.remove(moudle);
}
public List<IMoudle> getChildrenMoudleList() {
return childrenMoudleList;
}
public void setChildrenMoudleList(List<IMoudle> childrenMoudleList) {
this.childrenMoudleList = childrenMoudleList;
}
}
public class Battery extends Moudle {
public Battery(int price, String name) {
super(price,name);
}
}
public class CPU extends Moudle {
public CPU(int price, String name) {
super(price,name);
}
}
public class Phone extends Moudle {
public Phone(int price, String name) {
super(price,name);
}
}
public class PhoneMaker {
private static class PhoneMakerDelegate extends PhoneMaker{
public static PhoneMaker maker=new PhoneMakerDelegate();
}
private PhoneMaker() {
}
public static PhoneMaker instance() {
return PhoneMakerDelegate.maker;
}
public IMoudle getMyPhone(String cpuName,Integer batteryCap){
IMoudle phone=new Phone(100,"Iphone");
IMoudle cpu=new CPU(300,cpuName);
IMoudle battery=new Battery(batteryCap,"battery");
phone.add(cpu);
phone.add(battery);
return phone;
}
}
public class App {
public static void main(String[] args) {
IMoudle phone=PhoneMaker.instance().getMyPhone("A8",5000);
System.out.println(JSON.toJSONString(phone));
}
}
文章目录
  1. 外观模式