外观模式

自闭症网瘾萝莉.ら 提交于 2019-12-21 11:33:41

定义:

提供了一个统一的接口,用于访问子系统一群功能的相关接口。就是将一群子系统,或者对象,或者接口等继续统筹,分类,组装成统一的接口,解耦内部和外部,同时降低复杂度。通俗的说,就是在一群接口外面再包一层。

图示:

案例分析:

假如我们要进行炒菜,这个过程中,对象有煤气罐(GasCylinder),炉灶(KitchenRange),食物(food),佐料(condiment)。

那么操作过程为:

1-> 打开煤气(GasCylinder)

2-> 打开炉灶(KitchenRange)

3-> 下油(condiment)

4-> 下食物(food)

5-> 下盐(condiment)

6-> 关闭炉灶(KitchenRange)

7->关闭煤气(GasCylinder)

一个人要完成炒菜过程,需要协调多个对象,发生什么变更,可维护性变得很差,例如将煤气变成天然气,这时变动将变得超级复杂,从外部到内部需要调整,过于复杂,耦合性过于高。

经过分析,一个炒菜过程可以分为三个步骤,第一起火,第二炒菜,第三关火,那么可以设计一个对象,来协调封装这些复杂,炒菜的人只需要关系他要执行的三个步骤即可。

代码实例如下:

对象类:

public class Food {
    private static Food food = null;

    private Food() {
    };

    public static synchronized Food getFood() {
        if (food == null) {
            food = new Food();
        }
        return food;
    }

    public void AddFood() {
        System.out.println("Add Food");
    }

}

package com.ygk.facade;

public class Condiment {
    
    private static Condiment condiment = null;

    private Condiment() {
    };

    public static synchronized Condiment getCondiment() {
        if (condiment == null) {
            condiment = new Condiment();
        }
        return condiment;
    }
    
    
    public void AddCookingOil(){
        System.out.println("Add cooking oil");
    }

    public void  AddCookingSalt(){
        System.out.println("Add cooking Salt");
    }
}

package com.ygk.facade;

public class GasCylinder {
    private static GasCylinder gasCylinder = null;

    private GasCylinder() {
    };

    public static synchronized GasCylinder getGasCylinder() {
        if (gasCylinder == null) {
            gasCylinder = new GasCylinder();
        }
        return gasCylinder;
    }

    public void on() {
        System.out.println("GasCylinder ON");
    }

    public void off() {
        System.out.println("GasCylinder OFF");
    }

}

package com.ygk.facade;

public class KitchenRange {
    private static KitchenRange kitchenRange = null;

    private KitchenRange() {
    };

    public static synchronized KitchenRange getKitchenRange() {
        if (kitchenRange == null) {
            kitchenRange = new KitchenRange();
        }
        return kitchenRange;
    }

    public void on() {
        System.out.println("KitchenRange ON");
    }

    public void off() {
        System.out.println("KitchenRange OFF");
    }

}

外观类:

public class KitchenFacade {
    Food food = null;
    Condiment condiment = null;
    GasCylinder gasCylinder = null;
    KitchenRange kitchenRange = null;

    public KitchenFacade() {
        food = Food.getFood();
        condiment = Condiment.getCondiment();
        gasCylinder = GasCylinder.getGasCylinder();
        kitchenRange = KitchenRange.getKitchenRange();
    }

    public void begin() {
        gasCylinder.on();
        kitchenRange.on();
    }

    public void cook() {
        condiment.AddCookingOil();
        food.AddFood();
        condiment.AddCookingSalt();
    }

    public void end() {
        gasCylinder.off();
        kitchenRange.off();
    }
}

外部类:

public class Cook {
    public static void main(String[] args) {
        KitchenFacade kf=new KitchenFacade();
        kf.begin();
        kf.cook();
        kf.end();
    }

}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!