Java设计模式 2. 结构型模式 (4) 外观模式
外观模式(Facade Pattern)隐藏系统的复杂性,并向客户端提供了一个客户端可以访问系统的接口。
为子系统中的一组接口提供一个一致的界面,外观模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。
1. 外观模式
class="1. 接口">
package com.andrew.pattern0204.facade.model01;
public interface Shape {
void draw();
}
package com.andrew.pattern0204.facade.model01;
public class Rectangle implements Shape {
@Override
public void draw() {
System.out.println("Rectangle draw()");
}
}
package com.andrew.pattern0204.facade.model01;
public class Square implements Shape {
@Override
public void draw() {
System.out.println("Square draw()");
}
}
package com.andrew.pattern0204.facade.model01;
public class Circle implements Shape {
@Override
public void draw() {
System.out.println("Circle draw()");
}
}
package com.andrew.pattern0204.facade.model01;
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();
}
}
package com.andrew.pattern0204.facade.model01;
/**
* 1. 外观模式
*
* @author andrew
* @date 2018/07/05
*/
public class Client {
public static void main(String[] args) {
ShapeMaker shapeMaker = new ShapeMaker();
shapeMaker.drawCircle();
shapeMaker.drawRectangle();
shapeMaker.drawSquare();
}
}
运行结果:
Circle draw()
Rectangle draw()
Square draw()