1.应用程序可能用到的一些接口,分别列出如下:
1.1:
package com.facade.test;
public interface ServiceA {
public void methodA();
}
1.2:
package com.facade.test;
public interface ServiceB {
public void methodB();
}
1.3:
package com.facade.test;
public interface ServiceC {
public void methodC();
}
2.以上各个方法的具体实现分别如下,对应上面的顺序:
2.1:
package com.facade.test;
public class ServiceAImpl implements ServiceA {
@Override
public void methodA() {
// TODO Auto-generated method stub
System.out.println("method a is running......");
}
}
2.2:
package com.facade.test;
public class ServiceBImpl implements ServiceB {
@Override
public void methodB() {
// TODO Auto-generated method stub
System.out.println("method b is running......");
}
}
2.3:
package com.facade.test;
public class ServiceCImpl implements ServiceC {
@Override
public void methodC() {
// TODO Auto-generated method stub
System.out.println("method c is running......");
}
}
3.facade:把用到的接口都放到此类中,然后进行实例化,若要完成一个任务,需要用到其中某些对象的一些方法,则把这组相关方法组合起来,构成一个新的方法,这样的话,我们可以直接调用前面那个方法,从而完成某一个特定的功能:
package com.facade.test;
public class Facade {
ServiceA sa;
ServiceB sb;
ServiceC sc;
public Facade() {
sa = new ServiceAImpl();
sb = new ServiceBImpl();
sc = new ServiceCImpl();
}
public void methodA(){
sa.methodA();
sb.methodB();
}
public void methodB() {
sb.methodB();
sc.methodC();
}
public void methodC() {
sc.methodC();
sa.methodA();
}
}
4.test:测试程序,当单独使用特定类的对象时和使用外观模式时进行对比,从而可以看出相应的效果:
package com.facade.test;
public class Test {
public static void main(String[] args) {
ServiceA sa = new ServiceAImpl();
ServiceB sb = new ServiceBImpl();
sa.methodA();
sb.methodB();
System.out.println("=====================");
Facade f = new Facade();
f.methodA();
f.methodB();
}
}
5.运行结果如下:
method a is running......
method b is running......
=====================
method a is running......
method b is running......
method b is running......
method c is running......