File tree Expand file tree Collapse file tree 11 files changed +175
-0
lines changed
Expand file tree Collapse file tree 11 files changed +175
-0
lines changed Original file line number Diff line number Diff line change 1616单例模式:singleton<br >
1717命令模式:command<br >
1818适配器模式:adapter<br >
19+ 外观模式:facade<br >
1920<br ><br ><br ><br ><br ><br ><br ><br >
2021
2122
Original file line number Diff line number Diff line change 1+ 外观模式(Facade)
2+
3+ 一、定义
4+ 外部与一个子系统的通信通过一个统一的外观角色进行,为子系统中的一组接口提供一个一致的入口,外观模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。
5+
6+ 二、结构
7+ Facade(外观角色):在客户端可以调用这个角色的方法,在外观角色中可以知道相关的子系统的功能和责任;
8+ 在正常情况下,它将所有从客户端发来的请求委派到相应的子系统中去,传递给相应的子系统对象处理。
9+ SubSystem(子系统角色):在软件系统中可以有一个或者多个子系统角色,每一个子系统可以不是一个单独的类,而是一个类的集合,
10+ 它实现子系统的功能;子系统并不知道外观(又称为门面)的存在,对于子系统而言,外观角色仅仅是另一个客户端而已。
11+
12+ 三、优点
13+ 对客户端屏蔽了子系统组件,减少了客户端需要处理的对象数量并且使得子系统使用起来更加容易。
14+ 实现了子系统与客户端之间松耦合。
15+ 提供了一个访问子系统的统一入口,并不影响客户端直接使用子系统。
16+
17+ 四、缺点
18+ 使用合适的情况下没有什么问题
19+
20+ 五、应用场景
21+ 想要为访问一系列复杂的子系统提供一个统一的简单入口
22+ 客户端与多个子系统之间存在很大的依赖性,引入外观类可以将子系统和客户端解耦
23+ 在层次化结构中,可以使用外观模式定义系统中每一层的入口,层与层之间不直接产生联系
24+
25+ 六、个人总结
26+ 1、这个模式特别容易,这也是经常被使用的一种模式,很多时候我们都在使用,只是你可能还不知道它的名字而已。
27+ 2、通过一个外观从而让访问者可以避免直接访问过多子系统的接口,可以直接通过访问外观就能达到最终的目的。
28+ 3、如果你写过J2EE的项目,很多时候我们使用service调用多个dao达到目的,从另一个角度讲,service就是一种外观。
29+ 4、与适配器模式对比:
30+ 适配器模式是把一个原来无法访问的接口,通过适配让客户端能进行访问。
31+ 外观模式原来的接口可以正常的访问,只是封装了一个更简单的接口从而方便客户端的访问。
Original file line number Diff line number Diff line change 1+ package facade .nunuse ;
2+
3+ /**
4+ * 不使用外观模式的情况
5+ * Created by LinkinStar
6+ */
7+ public class MainTest {
8+
9+ public static void main (String [] args ) {
10+ SubSystemA subSystemA = new SubSystemA ();
11+ SubSystemB subSystemB = new SubSystemB ();
12+ SubSystemC subSystemC = new SubSystemC ();
13+
14+ subSystemA .stepOne ();
15+ subSystemB .stepOne ();
16+ subSystemC .stepOne ();
17+ subSystemA .stepTwo ();
18+ subSystemB .stepTwo ();
19+ subSystemC .stepTwo ();
20+
21+ /**
22+ * 遇到的问题:
23+ * 客户端的逻辑步骤过多,看起来逻辑过于复杂
24+ */
25+ }
26+ }
Original file line number Diff line number Diff line change 1+ package facade .nunuse ;
2+
3+ /**
4+ * 子系统A
5+ * Created by LinkinStar
6+ */
7+ public class SubSystemA {
8+ public void stepOne (){
9+ System .out .println ("步骤1" );
10+ }
11+ public void stepTwo (){
12+ System .out .println ("步骤4" );
13+ }
14+ }
Original file line number Diff line number Diff line change 1+ package facade .nunuse ;
2+
3+ /**
4+ * 子系统B
5+ * Created by LinkinStar
6+ */
7+ public class SubSystemB {
8+ public void stepOne (){
9+ System .out .println ("步骤2" );
10+ }
11+ public void stepTwo (){
12+ System .out .println ("步骤5" );
13+ }
14+ }
Original file line number Diff line number Diff line change 1+ package facade .nunuse ;
2+
3+ /**
4+ * 子系统C
5+ * Created by LinkinStar
6+ */
7+ public class SubSystemC {
8+ public void stepOne (){
9+ System .out .println ("步骤3" );
10+ }
11+ public void stepTwo (){
12+ System .out .println ("步骤6" );
13+ }
14+ }
Original file line number Diff line number Diff line change 1+ package facade .use ;
2+
3+ /**
4+ * 外观角色
5+ * Created by LinkinStar
6+ */
7+ public class Facade {
8+ public void execute (){
9+ SubSystemA subSystemA = new SubSystemA ();
10+ SubSystemB subSystemB = new SubSystemB ();
11+ SubSystemC subSystemC = new SubSystemC ();
12+
13+ subSystemA .stepOne ();
14+ subSystemB .stepOne ();
15+ subSystemC .stepOne ();
16+ subSystemA .stepTwo ();
17+ subSystemB .stepTwo ();
18+ subSystemC .stepTwo ();
19+ }
20+ }
Original file line number Diff line number Diff line change 1+ package facade .use ;
2+
3+ /**
4+ * 使用外观模式的情况
5+ * Created by LinkinStar
6+ */
7+ public class MainTest {
8+
9+ public static void main (String [] args ) {
10+ Facade facade = new Facade ();
11+ facade .execute ();
12+ }
13+ }
Original file line number Diff line number Diff line change 1+ package facade .use ;
2+
3+ /**
4+ * 子系统A
5+ * Created by LinkinStar
6+ */
7+ public class SubSystemA {
8+ public void stepOne (){
9+ System .out .println ("步骤1" );
10+ }
11+ public void stepTwo (){
12+ System .out .println ("步骤4" );
13+ }
14+ }
Original file line number Diff line number Diff line change 1+ package facade .use ;
2+
3+ /**
4+ * 子系统B
5+ * Created by LinkinStar
6+ */
7+ public class SubSystemB {
8+ public void stepOne (){
9+ System .out .println ("步骤2" );
10+ }
11+ public void stepTwo (){
12+ System .out .println ("步骤5" );
13+ }
14+ }
You can’t perform that action at this time.
0 commit comments