外观模式(Facade Pattern)隐藏系统的复杂性,并向客户端提供了一个客户端可以访问系统的接口。
思考: 外观模式和工厂模式的区别在于, 外观模式提供给客户端一个api, 然后外观模式来完成相应动作, 而工厂模式是提供给客户端产品, 然后客户端决定去做什么事情
package day0319.FacadePattern;
public class Demo{
public static void main(String[] args){
ShapeMaker shapeMaker = new ShapeMaker();
shapeMaker.drawCircle();
shapeMaker.drawRectangle();
shapeMaker.drawSquare();
}
}
class ShapeMaker {
Circle circle;
Rectangle rectangle;
Square square;
public ShapeMaker(){
circle = new Circle();
rectangle = new Rectangle();
square = new Square();
}
public void drawCircle() {
circle.draw();
}
public void drawSquare() {
square.draw();
}
public void drawRectangle() {
rectangle.draw();
}
}
interface Shape {
void draw();
}
class Circle implements Shape {
@Override
public void draw(){
System.out.println("画了一个圆");
}
}
class Rectangle implements Shape {
@Override
public void draw(){
System.out.println("画了一个四边形");
}
}
class Square implements Shape {
@Override
public void draw(){
System.out.println("画了一个矩形");
}
}