外观模式:其实就是对调用复杂的业务进行再一次封装,给客户一个简单接口API。
代码如下:
package com.mkf.pattern;public interface InterfaceOne { public void operationOne();}package com.mkf.pattern.impl;import com.mkf.pattern.InterfaceOne;public class ClassOne implements InterfaceOne { @Override public void operationOne() { System.out.println("复杂业务逻辑ONE " + ClassOne.class.getName()); }}package com.mkf.pattern;public interface InterfaceTwo { public void operationTwo();}package com.mkf.pattern.impl;import com.mkf.pattern.InterfaceTwo;public class ClassTwo implements InterfaceTwo { @Override public void operationTwo() { System.out.println("复杂业务逻辑TWO " + ClassTwo.class.getName()); }}package com.mkf.pattern;public interface InterfaceOther { public void operation();}package com.mkf.pattern.impl;import com.mkf.pattern.InterfaceOne;import com.mkf.pattern.InterfaceOther;import com.mkf.pattern.InterfaceTwo;public class ClassOther implements InterfaceOther { private InterfaceOne io; private InterfaceTwo it; public ClassOther() { this.io = new ClassOne(); this.it = new ClassTwo(); } @Override public void operation() { System.out.println("调用外观(封装)"); io.operationOne(); it.operationTwo(); }}package com.mkf;import com.mkf.pattern.InterfaceOther;import com.mkf.pattern.impl.ClassOther;public class TestFacade { /** * @param args */ public static void main(String[] args) { InterfaceOther other = new ClassOther(); other.operation(); }}
执行结果:
调用外观(封装)
复杂业务逻辑ONE com.mkf.pattern.impl.ClassOne 复杂业务逻辑TWO com.mkf.pattern.impl.ClassTwo