外观模式
内容参考 w3cschool
分类:结构性设计模式
应用:前台服务员的设定
目录
UML类图
按照教程上理解,就是将 ShapeMaker 看作是一个前台,你只需要告诉他:"我需要一个汉堡",然后汉堡就出来了。用户不用关心汉堡是怎么制作出来的,经历了那些流程。可以说是对后台细节的掩盖,用户不用经历繁琐的过程。
按照教程自己编写教程
创建形状接口
Shape
public interface Shape {
/**
* 绘制形状
*/
void draw();
}
创建形状实体类
Circle
public class Circle implements Shape{
@Override
public void draw() {
System.out.println("[Circle] Draw");
}
}
Square
public class Square implements Shape{
@Override
public void draw() {
System.out.println("[Square] Draw");
}
}
Rectangle
public class Rectangle implements Shape{
@Override
public void draw() {
System.out.println("[Rectangle] Draw");
}
}
创建外观
public class ShapeMaker {
private Shape circle;
private Shape square;
private Shape rectangle;
public ShapeMaker(){
circle = new Circle();
square = new Square();
rectangle = new Rectangle();
}
/**
* 绘制圆形
*/
public void drawCircle(){
circle.draw();
}
/**
* 绘制矩形
*/
public void drawSquare(){
square.draw();
}
/**
* 绘制三角形
*/
public void drawRectangle(){
rectangle.draw();
}
}
测试运行
public class ExecuteMain {
public static void main(String[] args) {
ShapeMaker shapeMaker = new ShapeMaker();
shapeMaker.drawCircle();
shapeMaker.drawSquare();
shapeMaker.drawRectangle();
}
}
[Circle] Draw
[Square] Draw
[Rectangle] Draw
Process finished with exit code 0