File tree Expand file tree Collapse file tree
java-des/src/com/java/design/facade Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 1+ package com .java .design .facade ;
2+
3+ public interface ServiceA {
4+
5+ void methodA ();
6+
7+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 1+ package com .java .design .facade ;
2+
3+ public interface ServiceB {
4+
5+ void methodB ();
6+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 1+ package com .java .design .facade ;
2+
3+ public interface ServiceC {
4+
5+ void methodC ();
6+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments