设计模式(12)--外观模式

来源:互联网 发布:简单的ps软件 编辑:程序博客网 时间:2024/06/08 18:40

什么是外观模式?
隐藏系统的复杂性,并向客户端提供了一个客户端可以访问系统的接口。这种类型的设计模式属于结构型模式,它向现有的系统添加一个接口,来隐藏系统的复杂性。

如何实现?

public  interface Shape{    void draw();}
public class Circle implements Shape {    public void draw() {        System.out.println("Shape: Circle");    }}
public class Rectangle implements Shape {    public void draw() {        System.out.println("Shape: Rectangle");    }}

外观模式实现:

public class ShapeMaker {    private Circle circle;    private Rectangle rectangle;    public  ShapeMaker(){        circle = new Circle();        rectangle = new Rectangle();    }    public void drawCircle(){        circle.draw();    }    public void drawRectangle(){        rectangle.draw();    }}

调用客户端:

public class Client {    public static void main(String[] agrs){        ShapeMaker maker = new ShapeMaker();        maker.drawCircle();        maker.drawRectangle();    }}
原创粉丝点击