Android的开发之&java23中设计模式------>外观模式

来源:互联网 发布:手机淘宝收货地址在哪 编辑:程序博客网 时间:2024/06/06 09:37
适用场景 (1)设计初期阶段,应该有意识的将不同层分离,层与层之间建立外观模式。 (2) 开发阶段,子系统越来越复杂,增加外观模式提供一个简单的调用接口。 (3) 维护一个大型遗留系统的时候,可能这个系统已经非常难以维护和扩展,但又包含非常重要的功能,为其开发一个外观类,以便新系统与其交互。

public class FacadeMethodActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_facade_method);        ShapeMaker shapeMaker=new ShapeMaker();        shapeMaker.drawCircle();        shapeMaker.drawRectangle();        shapeMaker.drawSquare();    }}
public class ShapeMaker {    private Shape circle,rectangle,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();    }}
public interface Shape {    void draw();}
public class Circle implements Shape {    @Override    public void draw() {        System.out.print("circle");    }}
public class Rectangle implements Shape {    @Override    public void draw() {        System.out.print("rectangle");    }}
public class Square implements Shape {    @Override    public void draw() {        System.out.print("square");    }}

优点 (1)实现了子系统与客户端之间的松耦合关系。 (2)客户端屏蔽了子系统组件,减少了客户端所需处理的对象数目,并使得子系统使用起来更加容易
github地址:https://github.com/zyj18410105150/DesignMode


原创粉丝点击