什么是外观模式?
隐藏系统的复杂性,并向客户端提供了一个客户端可以访问系统的接口。这种类型的设计模式属于结构型模式,它向现有的系统添加一个接口,来隐藏系统的复杂性。
如何实现?
public interface Shape{
void draw();
}
public class Circle implements Shape {
public void draw() {
System.out.println("Shape: Circle");
}
}
public class Rectangle implements Shape {
public void draw() {
System.out.println("Shape: Rectangle");
}
}
外观模式实现:
public class ShapeMaker {
private Circle circle;
private Rectangle rectangle;
public ShapeMaker(){
circle = new Circle();
rectangle = new Rectangle();
}
public void drawCircle(){
circle.draw();
}
public void drawRectangle(){
rectangle.draw();
}
}
调用客户端:
public class Client {
public static void main(String[] agrs){
ShapeMaker maker = new ShapeMaker();
maker.drawCircle();
maker.drawRectangle();
}
}