意图:为子系统中的一组接口提供一个一致的界面,外观模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。

主要解决:降低访问复杂系统的内部子系统时的复杂度,简化客户端与之的接口。
实现:

\"在这里插入图片描述\"
接口:
public interface Shape {
void draw();
}
实现类:
public class Circle implements Shape {
@Override
public void draw() {
// TODO Auto-generated method stub
System.out.println(“Circle”);
}

}
public class Retangle implements Shape {

@Override
public void draw() {
	System.out.println(\"Retangle\");
}

}
public class Square implements Shape {

@Override
public void draw() {
	System.out.println(\"Square\");
}

}
创建一个外观类;
public class ShapeMaker {
private Shape circle;
private Shape rectangle;
private Shape square;
public ShapeMaker(){
circle=new Circle();
rectangle =new Retangle();
square=new Square();
}
public void drawCircle(){
circle.draw();
}
public void drawRetangle(){
rectangle.draw();
}
public void drawSquare(){
square.draw();
}
}
测试:
public class test {

public static void main(String[] args) {
	ShapeMaker shapeMaker=new ShapeMaker();
	shapeMaker.drawCircle();
	shapeMaker.drawRetangle();
	shapeMaker.drawSquare();
}

}
结果:
Circle
Retangle
Square

收藏 打印