為子系統中的一組接口提供一個一致的界面,Facade模式定義了一個高層接口,這個接口使得這一子系統更加容易使用。
public interface Shape {
public void draw();
}
public class Circle implements Shape{
@Override
public void draw() {
System.out.println("畫一個圓形");
}
}
public class Square implements Shape{
@Override
public void draw() {
System.out.println("畫一個方形");
}
}
public class Triangle implements Shape{
@Override
public void draw() {
System.out.println("畫一個三角形");
}
}
public class ShapeMaker {
private Circle circle;
private Square square;
private Triangle triangle;
public ShapeMaker(){
circle = new Circle();
square = new Square();
triangle = new Triangle();
}
public void drawCircle(){
circle.draw();
}
public void drawSquare(){
square.draw();
}
public void drawTriangle(){
triangle.draw();
}
}
public class FacadePatternDemo {
public static void main(String[] args) {
ShapeMaker shapeMaker = new ShapeMaker();
shapeMaker.drawCircle();
shapeMaker.drawSquare();
shapeMaker.drawTriangle();
}
}
畫一個圓形
畫一個方形
畫一個三角形