- 设计模式教程
- 设计模式 - 主页
- 设计模式 - 概述
- 设计模式-工厂模式
- 抽象工厂模式
- 设计模式-单例模式
- 设计模式-构建器模式
- 设计模式 - 原型模式
- 设计模式-适配器模式
- 设计模式-桥接模式
- 设计模式-过滤器模式
- 设计模式-复合模式
- 设计模式 - 装饰模式
- 设计模式-外观模式
- 设计模式-享元模式
- 设计模式-代理模式
- 责任链模式
- 设计模式-命令模式
- 设计模式-解释器模式
- 设计模式-迭代器模式
- 设计模式——中介者模式
- 设计模式 - 纪念品模式
- 设计模式-观察者模式
- 设计模式-状态模式
- 设计模式-空对象模式
- 设计模式-策略模式
- 设计模式-模板模式
- 设计模式-访客模式
- 设计模式-MVC模式
- 业务代表模式
- 复合实体模式
- 数据访问对象模式
- 前控制器模式
- 拦截过滤器模式
- 服务定位器模式
- 传输对象模式
设计模式-外观模式
外观模式隐藏了系统的复杂性,并向客户端提供了一个接口,客户端可以使用该接口访问系统。这种类型的设计模式属于结构模式,因为这种模式向现有系统添加了一个接口以隐藏其复杂性。
此模式涉及单个类,该类提供客户端所需的简化方法并将调用委托给现有系统类的方法。
执行
我们将创建一个Shape接口和实现Shape接口的具体类。下一步定义外观类ShapeMaker 。
ShapeMaker类使用具体类将用户调用委托给这些类。FacadePatternDemo,我们的演示类,将使用ShapeMaker类来显示结果。
步骤1
创建一个界面。
形状.java
public interface Shape {
void draw();
}
第2步
创建实现相同接口的具体类。
矩形.java
public class Rectangle implements Shape {
@Override
public void draw() {
System.out.println("Rectangle::draw()");
}
}
方形.java
public class Square implements Shape {
@Override
public void draw() {
System.out.println("Square::draw()");
}
}
圆.java
public class Circle implements Shape {
@Override
public void draw() {
System.out.println("Circle::draw()");
}
}
步骤3
创建一个外观类。
ShapeMaker.java
public class ShapeMaker {
private Shape circle;
private Shape rectangle;
private Shape square;
public ShapeMaker() {
circle = new Circle();
rectangle = new Rectangle();
square = new Square();
}
public void drawCircle(){
circle.draw();
}
public void drawRectangle(){
rectangle.draw();
}
public void drawSquare(){
square.draw();
}
}
步骤4
使用立面绘制各种类型的形状。
FacadePatternDemo.java
public class FacadePatternDemo {
public static void main(String[] args) {
ShapeMaker shapeMaker = new ShapeMaker();
shapeMaker.drawCircle();
shapeMaker.drawRectangle();
shapeMaker.drawSquare();
}
}
步骤5
验证输出。
Circle::draw() Rectangle::draw() Square::draw()