facaed pattern 门面模式

为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口使得这一子系统更加容易使用

Facade 外观类:知道哪些子系统类负责处理请求,将客户的请求代理给适当的子系统对象.熟悉子系统的功能。内部根据客户角色的需求预定了几种功能的组合。

SubSystem:子系统类集合 实现子系统的功能,处理Facade对象指派的任务.注意子类中没有Facade的任何信息,即没有对Facde对象的引用,对于Facade类和客户端来说是未知的,一般较为复杂.

外观模式的使用场景:

1.在设计初期阶段,应该要有意识的将不同的两个层分离,如MVC可以在层与层之间建立外观Facade,这样可以为复杂的子系统提供一个简单的接口,使耦合大大降低

2.在开发阶段,子系统往往因为不断的重构演化而变得越来越复杂,增加Facade可以提供一个简单的接口,减少它们之间的依赖.

3.在维护一个遗留的大型系统时,可能这个系统已经非常难以维护和扩展了,但新的需求开发必须要依赖于它,此时用Facade模式也是非常合适的.

可以为新系统开发一个外观Facade类,来提供设计粗糙或高度复杂的遗留代码的比较清晰简单的接口,让新系统和Facade对象,Facade与遗留代码交互所有复杂的工作.

以电脑为例 电脑中有CPU DISK Memory ,用户使用电脑并不需要关心子零件 只需要通过Facade类即可

/**

  • 门面类(核心)

  • @author guk

*/

public class Computer {

private CPU cpu;

private Memory memory;

private Disk disk;

public Computer() {

cpu=new CPU();

disk=new Disk();

memory=new Memory();

}

public void start() {

System.out.println(“computer start begin”);

cpu.start();

disk.start();

memory.start();

System.out.println(“computer start end”);

}

public void shutdown() {

System.out.println(“computer shutdown begin”);

cpu.shutdown();

disk.shutdown();

memory.shutdown();

System.out.println(“computer shutdown end”);

}

}

public static void main(String[] args) {

Computer computer=new Computer();

computer.start();

computer.shutdown();

System.out.println(Computer.class.getClassLoader());

}

/**

  • cpu子系统类

  • @author guk

*/

public class CPU {

public void start() {

System.out.println(“cpu is starting…”);

}

public void shutdown() {

System.out.println(“CPU is shutdown…”);

}

}

/**

  • cpu子系统类

  • @author guk

*/

public class Disk {

public void start() {

System.out.println(“disk is starting…”);

}

public void shutdown() {

System.out.println(“disk is shutdown…”);

}

}

/**

  • cpu子系统类

  • @author guk

*/

public class Memory {

public void start() {

System.out.println(“Memory is starting…”);

}

public void shutdown() {

System.out.println(“Memory is shutdown…”);

}

}

转载出处:

作者「rdgk」

原文链接:https://blog.csdn.net/qq_380059

发布了4 篇原创文章 · 获赞 0 · 访问量 295

猜你喜欢

转载自blog.csdn.net/TIANTIAN_ZZ/article/details/103927405