Java--設計模式之外觀模式

外觀模式,爲子系統中的一組接口提供一個一致的界面,此模式定義了一個高層的接口,這個接口似的這一子系統更加容易使用。看例子就明白啦。

1、子系統三個類

public class SubSystemOne {
	public void methodOne(){
		System.out.println("子系統方法一");
	}
}
public class SubSystemTwo {
	public void methodTwo(){
		System.out.println("子系統方法二");
	}
}

public class SubSystemThree {
	public void methodThree(){
		System.out.println("子系統方法三");
	}
}

2、外觀類
public class Facade {
	SubSystemOne one;
	SubSystemTwo two;
	SubSystemThree three;
	public Facade(){
		one = new SubSystemOne();
		two = new SubSystemTwo();
		three = new SubSystemThree();
	}
	public void methodA(){
		one.methodOne();
		two.methodTwo();
		three.methodThree();
	}
	public void methodB(){
		two.methodTwo();
		three.methodThree();
	}
}
3、測試
public class Test {
	public static void main(String[] args) {
		Facade facade = new Facade();
		System.out.println("方法A");
		facade.methodA();
		System.out.println("方法B");
		facade.methodB();
	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章