外观模式隐藏系统的复杂性,并向客户端提供了一个客户端可以访问系统的接口。这种模式涉及到一个单一的类,该类提供了客户端请求的简化方法和对现有系统类方法的委托调用。
例子:通过ShapeMaker简化Shape的创建。
1创建Shape类
1 2 3 4 5 6 7 8
| package com.notejava.facade;
public interface Shape { void draw(); }
|
2.创建Circle类
1 2 3 4 5 6 7 8 9 10 11
| package com.notejava.facade;
public class Circle implements Shape { @Override public void draw() {
} }
|
3.创建Rectangle类
1 2 3 4 5 6 7 8 9 10 11
| package com.notejava.facade;
public class Rectangle implements Shape { @Override public void draw() {
} }
|
4.创建Square类
1 2 3 4 5 6 7 8 9 10 11
| package com.notejava.facade;
public class Square implements Shape { @Override public void draw() {
} }
|
5.创建ShapeMaker类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| package com.notejava.facade;
public class ShapeMaker { protected Shape circle; protected Shape rectangle; protected Shape square;
public ShapeMaker() { this.circle = new Circle(); this.rectangle = new Rectangle(); this.square = new Square(); }
public void drawCircle() {
}
public void drawRectangle() {
}
public void drawSquare() {
} }
|
6.创建Demo类
1 2 3 4 5 6 7 8 9 10 11 12 13
| package com.notejava.facade;
public class Demo { public static void main(String\[\] args) { ShapeMaker shapeMaker = new ShapeMaker(); shapeMaker.drawCircle(); shapeMaker.drawRectangle(); shapeMaker.drawSquare(); } }
|