外观模式(FacadePattern)隐藏系统的复杂性,并向客户端提供了一个客户端可以访问系统的接口。这种类型的设计模式属于结构型模式,它向现有的系统添加一个接口,来隐藏系统的复杂性。
优点:
1、减少系统相互依赖。
2、提高灵活性。
3、提高了安全性。
缺点:
不符合开闭原则,如果要改东西很麻烦,继承重写都不合适。
应用:
降低访问复杂系统的内部子系统时的复杂度,简化客户端与之的接口。
1、为复杂的模块或子系统提供外界访问的模块。
2、子系统相对独立。
3、预防低水平人员带来的风险。
![](https://i-blog.csdnimg.cn/blog_migrate/7ba43c451875604132a16071f396b5bb.png)
实现代码
public interface IShape {
void draw();
}
public class CircleImpl implements IShape {
@Override
public void draw() {
System.out.println("Circle::draw()");
}
}
public class RectangleImpl implements IShape {
@Override
public void draw() {
System.out.println("Rectangle::draw()");
}
}
public class SquareImpl implements IShape {
@Override
public void draw() {
System.out.println("Square::draw()");
}
}
public class ShapeMaker {
private IShape circle;
private IShape rectangle;
private IShape square;
public ShapeMaker() {
circle = new CircleImpl();
rectangle = new RectangleImpl();
square = new SquareImpl();
}
public void drawCircle() {
circle.draw();
}
public void drawRectangle() {
rectangle.draw();
}
public void drawSquare() {
square.draw();
}
}
public static void main(String[] args) {
ShapeMaker shapeMaker = new ShapeMaker();
shapeMaker.drawCircle();
shapeMaker.drawRectangle();
shapeMaker.drawSquare();
}