一,定义
就是把一堆类给放在一个方法(A)中去初始化,然后在A中写方法去组装那一堆类中的方法,对外所有的方法只调用A中的方法,以后只关心A中的方法,不用管那一堆类中的方法了
二,代码
- 2.1,目录
- 2.2,代码
package com.boot.lijunhao.waiguan;/** * 外观模式入口 * Created by lijunhao on 2017/8/29. */public class InClass { private MethodClassA methodClassA; private MethodClassB methodClassB; private MethodClassC methodClassC; public InClass() { //实例化所有的MethodClassA,MethodClassB,MethodClassC this.methodClassA = new MethodClassA(); this.methodClassB = new MethodClassB(); this.methodClassC = new MethodClassC(); } //ABCD方法随意组合 --start public void methodA(){ methodClassA.Amethod(); methodClassB.Amethod(); methodClassC.Cmethod(); } public void methodB(){ methodClassA.Amethod(); methodClassB.Amethod(); } public void methodC(){ methodClassA.Dmethod(); methodClassB.Dmethod(); methodClassC.Cmethod(); } public void methodD(){ methodClassA.Dmethod(); } //ABCD方法随意组合 --start}
package com.boot.lijunhao.waiguan;import lombok.extern.slf4j.Slf4j;/** * Created by lijunhao on 2017/8/29. */@Slf4jpublic class MethodClassA { public void Amethod(){ log.info("Amethod在类(MethodClassA.java:13)行:{}","A类中的方法a"); } public void Bmethod(){ log.info("Bmethod在类(MethodClassA.java:17)行:{}","A类中的方法b"); } public void Cmethod(){ log.info("Cmethod在类(MethodClassA.java:21)行:{}","A类中的方法c"); } public void Dmethod(){ log.info("Dmethod在类(MethodClassA.java:25)行:{}","A类中的方法d"); }}
package com.boot.lijunhao.waiguan;import lombok.extern.slf4j.Slf4j;/** * Created by lijunhao on 2017/8/29. */@Slf4jpublic class MethodClassB { public void Amethod(){ log.info("Amethod在类(MethodClassB.java:13)行:{}","B类中的方法a"); } public void Bmethod(){ log.info("Bmethod在类(MethodClassB.java:17)行:{}","B类中的方法b"); } public void Cmethod(){ log.info("Cmethod在类(MethodClassB.java:21)行:{}","B类中的方法c"); } public void Dmethod(){ log.info("Dmethod在类(MethodClassB.java:25)行:{}","B类中的方法d"); }}
package com.boot.lijunhao.waiguan;import lombok.extern.slf4j.Slf4j;/** * Created by lijunhao on 2017/8/29. */@Slf4jpublic class MethodClassC { public void Amethod(){ log.info("Amethod在类(MethodClassC.java:13)行:{}","C类中的方法a"); } public void Bmethod(){ log.info("Bmethod在类(MethodClassC.java:17)行:{}","C类中的方法b"); } public void Cmethod(){ log.info("Cmethod在类(MethodClassC.java:21)行:{}","C类中的方法c"); } public void Dmethod(){ log.info("Dmethod在类(MethodClassC.java:25)行:{}","C类中的方法d"); }}
package com.boot.lijunhao.waiguan;import lombok.extern.slf4j.Slf4j;/** * Created by lijunhao on 2017/8/29. */@Slf4jpublic class Start { public static void main(String[] args) { //所有的入口都是这个InClass,用其里面的方法去调用 InClass inClass = new InClass(); inClass.methodA(); log.info("main在类(Start.java:16)行:{}","A方法结束"); inClass.methodB(); log.info("main在类(Start.java:18)行:{}","B方法结束"); inClass.methodC(); log.info("main在类(Start.java:20)行:{}","C方法结束"); inClass.methodD(); log.info("main在类(Start.java:22)行:{}","D方法结束"); }}
- 2.3,结果
13:29:56.344 [main] INFO com.boot.lijunhao.waiguan.MethodClassA - Amethod在类(MethodClassA.java:13)行:A类中的方法a13:29:56.348 [main] INFO com.boot.lijunhao.waiguan.MethodClassB - Amethod在类(MethodClassB.java:13)行:B类中的方法a13:29:56.349 [main] INFO com.boot.lijunhao.waiguan.MethodClassC - Cmethod在类(MethodClassC.java:21)行:C类中的方法c13:29:56.349 [main] INFO com.boot.lijunhao.waiguan.Start - main在类(Start.java:16)行:A方法结束13:29:56.349 [main] INFO com.boot.lijunhao.waiguan.MethodClassA - Amethod在类(MethodClassA.java:13)行:A类中的方法a13:29:56.349 [main] INFO com.boot.lijunhao.waiguan.MethodClassB - Amethod在类(MethodClassB.java:13)行:B类中的方法a13:29:56.349 [main] INFO com.boot.lijunhao.waiguan.Start - main在类(Start.java:18)行:B方法结束13:29:56.349 [main] INFO com.boot.lijunhao.waiguan.MethodClassA - Dmethod在类(MethodClassA.java:25)行:A类中的方法d13:29:56.349 [main] INFO com.boot.lijunhao.waiguan.MethodClassB - Dmethod在类(MethodClassB.java:25)行:B类中的方法d13:29:56.349 [main] INFO com.boot.lijunhao.waiguan.MethodClassC - Cmethod在类(MethodClassC.java:21)行:C类中的方法c13:29:56.349 [main] INFO com.boot.lijunhao.waiguan.Start - main在类(Start.java:20)行:C方法结束13:29:56.349 [main] INFO com.boot.lijunhao.waiguan.MethodClassA - Dmethod在类(MethodClassA.java:25)行:A类中的方法d13:29:56.349 [main] INFO com.boot.lijunhao.waiguan.Start - main在类(Start.java:22)行:D方法结束