ErBing

往事已经定格,未来还要继续。

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

来源:http://www.bjsxt.com/ 
一、【GOF23设计模式】_外观模式、公司注册流程、迪米特法则

迪米特法则(最少知识原则)

外观模式核心

不使用外观模式

 1 package com.test.facade;
 2 
 3 public interface 工商局 {
 4     void checkName();//核名
 5 }
 6 
 7 class 海淀区工商局 implements 工商局 {
 8     @Override
 9     public void checkName() {
10         System.out.println("检查名字是否有冲突!");
11     }
12 }
 1 package com.test.facade;
 2 
 3 public interface 税务局 {
 4     void taxCertificate();//办理税务登记证
 5 }
 6 
 7 class 海淀区税务局 implements 税务局 {
 8     @Override
 9     public void taxCertificate() {
10         System.out.println("在海淀区税务局办理税务登记证!");
11     }
12 }
 1 package com.test.facade;
 2 
 3 public interface 银行 {
 4     void openAccount();//开户
 5 }
 6 
 7 class 中国工商银行 implements 银行 {
 8     @Override
 9     public void openAccount() {
10         System.out.println("在中国工商银行开户!");
11     }
12 }
 1 package com.test.facade;
 2 
 3 public interface 质检局 {
 4     void orgCodeCertificate();//办理组织机构代码证
 5 }
 6 
 7 class 海淀区质检局 implements 质检局 {
 8     @Override
 9     public void orgCodeCertificate() {
10         System.out.println("在海淀区质检局办理组织机构代码证!");
11     }
12 }
 1 package com.test.facade;
 2 /**
 3  * 不使用外观模式
 4  */
 5 public class Client1 {
 6     public static void main(String[] args) {
 7         工商局 a = new 海淀区工商局();
 8         a.checkName();
 9         质检局 b = new 海淀区质检局();
10         b.orgCodeCertificate();
11         税务局 c = new 海淀区税务局();
12         c.taxCertificate();
13         银行 d = new 中国工商银行();
14         d.openAccount();
15     }
16 }

使用外观模式

 1 package com.test.facade;
 2 /**
 3  * 办理注册公司流程的门面对象
 4  */
 5 public class RegisterFacade {
 6     public void register(){
 7         工商局 a = new 海淀区工商局();
 8         a.checkName();
 9         质检局 b = new 海淀区质检局();
10         b.orgCodeCertificate();
11         税务局 c = new 海淀区税务局();
12         c.taxCertificate();
13         银行 d = new 中国工商银行();
14         d.openAccount();
15     }
16 }
 1 package com.test.facade;
 2 /**
 3  * 使用外观模式
 4  */
 5 public class Client2 {
 6     public static void main(String[] args) {
 7 //      工商局 a = new 海淀区工商局();
 8 //      a.checkName();
 9 //      质检局 b = new 海淀区质检局();
10 //      b.orgCodeCertificate();
11 //      税务局 c = new 海淀区税务局();
12 //      c.taxCertificate();
13 //      银行 d = new 中国工商银行();
14 //      d.openAccount();
15 
16         new RegisterFacade().register();
17     }
18 }

开发中常见的场景

posted on 2016-08-24 12:52  ErBing  阅读(321)  评论(0编辑  收藏  举报