10000 外观模式 · Folgerjun/DesignPatterns-Java@f873122 · GitHub
[go: up one dir, main page]

Skip to content

Commit f873122

Browse files
authored
外观模式
外观模式
1 parent 3134fb3 commit f873122

File tree

8 files changed

+117
-0
lines changed

8 files changed

+117
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.java.design.facade;
2+
3+
public class Facade {
4+
5+
ServiceA serviceA;
6+
7+
ServiceB serviceB;
8+
9+
ServiceC serviceC;
10+
11+
public Facade() {
12+
13+
serviceA = new ServiceAImpl();
14+
serviceB = new ServiceBImpl();
15+
serviceC = new ServiceCImpl();
16+
}
17+
18+
public void methodA() {
19+
// 重组
20+
serviceA.methodA();
21+
serviceB.methodB();
22+
}
23+
24+
public void methodB() {
25+
// 重组
26+
serviceB.methodB();
27+
serviceC.methodC();
28+
}
29+
30+
public void methodC() {
31+
// 重组
32+
serviceC.methodC();
33+
serviceA.methodA();
34+
}
35+
36+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.java.design.facade;
2+
3+
/**
4+
* 外观模式 ----> 是一种使用频率非常高的结构型设计模式,它通过引入一个外观角色来简化客户端与子系统之间的交互,为复杂的子系统调用提供一个统一的入口,
5+
* 降低子系统与客户端的耦合度,且客户端调用非常方便。
6+
*
7+
* @author Administrator
8+
*
9+
*/
10+
public class FacadePattern {
11+
12+
public static void main(String[] args) {
13+
14+
// 不根据外观调用
15+
ServiceA serviceA = new ServiceAImpl();
16+
ServiceB serviceB = new ServiceBImpl();
17+
ServiceC serviceC = new ServiceCImpl();
18+
19+
serviceA.methodA();
20+
serviceB.methodB();
21+
serviceC.methodC();
22+
23+
System.out.println("----------------");
24+
25+
// 利用外观调用
26+
Facade facade = new Facade();
27+
facade.methodA();
28+
facade.methodB();
29+
facade.methodC();
30+
}
31+
32+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.java.design.facade;
2+
3+
public interface ServiceA {
4+
5+
void methodA();
6+
7+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.java.design.facade;
2+
3+
public class ServiceAImpl implements ServiceA {
4+
5+
@Override
6+
public void methodA() {
7+
8+
System.out.println("This is Method A ...");
9+
}
10+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.java.design.facade;
2+
3+
public interface ServiceB {
4+
5+
void methodB();
6+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.java.design.facade;
2+
3+
public class ServiceBImpl implements ServiceB {
4+
5+
@Override
6+
public void methodB() {
7+
8+
System.out.println("This is Method B ...");
9+
}
10+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.java.design.facade;
2+
3+
public interface ServiceC {
4+
5+
void methodC();
6+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.java.design.facade;
2+
3+
public class ServiceCImpl implements ServiceC {
4+
5+
@Override
6+
public void methodC() {
7+
8+
System.out.println("This is Method C ...");
9+
}
10+
}

0 commit comments

Comments
 (0)
0