设计模式之外观模式

设计模式Java大约 2917 字

作用

又叫门面模式、过程模式。定义一个接口,聚合了内部子系统的细节,使调用端只需调用这个接口,无需关心子系统内部细节。类似方法抽取;工具类。

注意:不能过多或不合理的使用外观模式,而应考虑使系统有层次,利于维护。

案例

各个子系统或模块

public class DVDPlayer {

    public void play() {
        System.out.println("DVDPlayer play");
    }

    public void pause() {
        System.out.println("DVDPlayer pause");
    }

}

public class Light {

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

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

}

public class Screen {

    public void up() {
        System.out.println("screen up");
    }

    public void down() {
        System.out.println("screen down");
    }

}

外观类

public class Facade {

    private DVDPlayer dvdPlayer = new DVDPlayer();
    private Screen screen = new Screen();
    private Light light = new Light();

    public void start() {
        dvdPlayer.play();
        screen.down();
        light.off();
    }

    public void pause() {
        dvdPlayer.pause();
    }

    public void end() {
        screen.up();
        light.on();
    }

}

调用

public class Client {

    public static void main(String[] args) {
        Facade facade = new Facade();
        facade.start();
        System.out.println("-----------");
        facade.pause();
        System.out.println("-----------");
        facade.end();
    }

}

源码

org.apache.ibatis.session.Configuration#newMetaObject

org.apache.ibatis.reflection.MetaObject#forObject

public class MetaObject {
    private Object originalObject;
    private ObjectWrapper objectWrapper;
    private ObjectFactory objectFactory;
    private ObjectWrapperFactory objectWrapperFactory;
    private ReflectorFactory reflectorFactory;

    private MetaObject(Object object, ObjectFactory objectFactory, ObjectWrapperFactory objectWrapperFactory, ReflectorFactory reflectorFactory) {
        this.originalObject = object;
        this.objectFactory = objectFactory;
        this.objectWrapperFactory = objectWrapperFactory;
        this.reflectorFactory = reflectorFactory;
        if (object instanceof ObjectWrapper) {
            this.objectWrapper = (ObjectWrapper)object;
        } else if (objectWrapperFactory.hasWrapperFor(object)) {
            this.objectWrapper = objectWrapperFactory.getWrapperFor(this, object);
        } else if (object instanceof Map) {
            this.objectWrapper = new MapWrapper(this, (Map)object);
        } else if (object instanceof Collection) {
            this.objectWrapper = new CollectionWrapper(this, (Collection)object);
        } else {
            this.objectWrapper = new BeanWrapper(this, object);
        }

    }

    public static MetaObject forObject(Object object, ObjectFactory objectFactory, ObjectWrapperFactory objectWrapperFactory, ReflectorFactory reflectorFactory) {
        return object == null ? SystemMetaObject.NULL_META_OBJECT : new MetaObject(object, objectFactory, objectWrapperFactory, reflectorFactory);
    }
}
阅读 211 · 发布于 2019-12-19

————        END        ————

扫描下方二维码关注公众号和小程序↓↓↓

昵称:
随便看看换一批