外观模式(Facade):
是为了解决类与类之间的依赖关系的,像spring一样,可以将类与类之间的关系配置到配置文件中,而外观模式就是将他们的关系放在一个Facade类中,降低了类与类之间的耦合度,该模式中没有涉及到接口。
UML图:
代码如下:
public class Client {
public static void main(String[] args) {
Facade facade=new Facade();
facade.M1();
System.out.println("=============");
facade.M2();
}
}
public class Facade {
private Method1 m1 = new Method1();
private Method2 m2 = new Method2();
public void M1(){
m1.foo();
m2.foo();
}
public void M2(){
m2.foo();
m1.foo();
}
}
public class Method1 {
public void foo(){
System.out.println(getClass().toString()+"\t"+Thread.currentThread().getStackTrace()[1].getMethodName());
}
}
public class Method2 {
public void foo(){
System.out.println(getClass().toString()+"\t"+Thread.currentThread().getStackTrace()[1].getMethodName());
}
}