门面模式 Facade Pattern

类似模式: 中介者模式

一、定义:
门面模式(Facade Pattern),是指提供一个统一的接口去访问多个子系统的多个不同的接口,它为子系统中的一组接口提供一个统一的高层接口。使得子系统更容易使用。

类似模式:......

外观模式包含如下两个角色:

Facade(外观角色):在客户端可以调用它的方法,在外观角色中可以知道相关的(一个或者多个)子系统的功能和责任;在正常情况下,它将所有从客户端发来的请求委派到相应的子系统去,传递给相应的子系统对象处理。

SubSystem(子系统角色):在软件系统中可以有一个或者多个子系统角色,每一个子系统可以不是一个单独的类,而是一个类的集合,它实现子系统的功能;每一个子系统都可以被客户端直接调用,或者被外观角色调用,它处理由外观类传过来的请求;子系统并不知道外观的存在,对于子系统而言,外观角色仅仅是另外一个客户端而已。

外观模式的目的不是给予子系统添加新的功能接口,而是为了让外部减少与子系统内多个模块的交互,松散耦合,从而让外部能够更简单地使用子系统。

外观模式的本质是:封装交互,简化调用

泡茶---->>>水  茶  热水壶  泡茶人     

实现方式1----- 自己泡茶  每个人单独的泡茶逻辑

实现方式1----- 茶馆泡茶  茶馆facade

二、门面模式的作用:
参考文章:
The facade pattern (also spelled façade) is a software-design pattern commonly used with object-oriented programming. Analogous to a facade in architecture, a facade is an object that serves as a front-facing interface masking more complex underlying or structural code. A facade can:

1、improve the readability and usability of a software library by masking interaction with more complex components behind a single (and often simplified) API;
2、provide a context-specific interface to more generic functionality (complete with context-specific input validation);
3、serve as a launching point for a broader refactor of monolithic or tightly-coupled systems in favor of more loosely-coupled code;

Developers often use the facade design pattern when a system is very complex or difficult to understand because the system has a large number of interdependent classes or because its source code is unavailable. This pattern hides the complexities of the larger system and provides a simpler interface to the client. It typically involves a single wrapper class that contains a set of members required by the client. These members access the system on behalf of the facade client and hide the implementation details.

简而言之就是门面模式主要有三个作用:
提高了可读性和可用性,屏蔽系统内部复杂的逻辑;
提供了一个更通用的接口;
支持更多松散耦合代码;

三、UML图:


客户端的调用变得非常简单明了。四、设计模式使用实例:
1、设计组成一台电脑的各个组件:

/*
 *  电脑CPU
 */
public class CPU {

    public void freeze() {

        //...

    }

    public void jump(long position) {

        //...

    }

    public void execute() {

        //...}

    }

/*
 *  硬件驱动
 */
class HardDrive {
    public byte[] read(long lba, int size) {
        //...
    }
}

/*
 *  内存
 */
class Memory {
    public void load(long position, byte[] data) { ... }
}


2、抽取一个门面接口,便于扩展(不仅仅在于电脑的启动,手机和Pad亦然如此)

/*
 *  抽取一个门面接口
 */

interface IComputerFacede{

    public void start();

}



3、实现真正被客户端调用的门面对象

/*
 *  门面对象
 */

class ComputerFacade implements IComputerFacede {
    private CPU cpu;
    private Memory memory;
    private HardDrive hardDrive;

    public ComputerFacade() {
        this.cpu = new CPU();
        this.memory = new Memory();
        this.hardDrive = new HardDrive();
    }

    public void start() {
        cpu.freeze();
        memory.load(BOOT_ADDRESS, hardDrive.read(BOOT_SECTOR, SECTOR_SIZE));
        cpu.jump(BOOT_ADDRESS);
        cpu.execute();
    }
}


4、客户端调用

/*
 *  客户端调用
 */
class Client {

    /* 只要简单的一个调用,就可以完成电脑的启动,无需了解内部逻辑 */
    public static void main(String[] args) {
        ComputerFacade computer = new ComputerFacade();
        computer.start();
    }
}



总结:由案例可以轻松的看出来,客户端仅需要和门面对象进行交互,无需了解计算机真正的启动流程,在降低系统耦合性的同时,也保障了内部系统的逻辑安全
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值