外观模式

✍ dations ◷ 2025-07-07 02:16:47 #软件设计模式

外观模式(Facade pattern),是软件工程中常用的一种软件设计模式,它为子系统中的一组接口提供一个统一的高层接口,使得子系统更容易使用。

FacadeDesignPattern.png

这是一个抽象的示例。一个客户“you”通过外观接口“computer”获取计算机内部复杂的系统信息。

/* Complex parts */class CPU {	public void freeze() { ... }	public void jump(long position) { ... }	public void execute() { ... }}class Memory {	public void load(long position, byte data) {		...	}}class HardDrive {	public byte read(long lba, int size) {		...	}}/* Façade */class Computer {	public void startComputer() {		cpu.freeze();		memory.load(BOOT_ADDRESS, hardDrive.read(BOOT_SECTOR, SECTOR_SIZE));		cpu.jump(BOOT_ADDRESS);		cpu.execute();	}}/* Client */class You {	public static void main(String args) {		Computer facade = new Computer();		facade.startComputer();	}}

C#

// Facade pattern -- Structural example using System;namespace DoFactory.GangOfFour.Facade.Structural{  // Mainapp test application   class MainApp  {    public static void Main()    {      Facade facade = new Facade();      facade.MethodA();      facade.MethodB();      // Wait for user       Console.Read();    }  }  // "Subsystem ClassA"   class SubSystemOne  {    public void MethodOne()    {      Console.WriteLine(" SubSystemOne Method");    }  }  // Subsystem ClassB"   class SubSystemTwo  {    public void MethodTwo()    {      Console.WriteLine(" SubSystemTwo Method");    }  }  // Subsystem ClassC"   class SubSystemThree  {    public void MethodThree()    {      Console.WriteLine(" SubSystemThree Method");    }  }  // Subsystem ClassD"   class SubSystemFour  {    public void MethodFour()    {      Console.WriteLine(" SubSystemFour Method");    }  }  // "Facade"   class Facade  {    SubSystemOne one;    SubSystemTwo two;    SubSystemThree three;    SubSystemFour four;    public Facade()    {      one = new SubSystemOne();      two = new SubSystemTwo();      three = new SubSystemThree();      four = new SubSystemFour();    }    public void MethodA()    {      Console.WriteLine("\nMethodA() ---- ");      one.MethodOne();      two.MethodTwo();      four.MethodFour();    }    public void MethodB()    {      Console.WriteLine("\nMethodB() ---- ");      two.MethodTwo();      three.MethodThree();    }  }}

C++

class CPU {public:	void freeze() { ... }	void jump(long position) { ... }	void execute() { ... }}class Memory {public:	void load(long position, char* data) {		...	}}class HardDrive {public:	char* read(long lba, int size) {		...	}}/* Façade */class Computer {public:	void startComputer() {		cpu.freeze();		memory.load(BOOT_ADDRESS, hardDrive.read(BOOT_SECTOR, SECTOR_SIZE));		cpu.jump(BOOT_ADDRESS);		cpu.execute();	}}/* Client */class You {public:	void start(String args) {		Computer facade = new Computer();		facade.startComputer();	}}


相关

  • 日本人日本人(日语:日本人/にほんじん/にっぽんじん Nihonjin / Nipponjin ?)是指拥有日本国籍者或日本列岛的各民族与族群。日本主体民族为大和民族,其他组成民族包括韩民族、满族、
  • 缬草酸戊酸是化学式为C5H10O2的短链饱和脂肪酸。其它常用中文名称还有正戊酸、缬草酸等。 与其他低相对分子质量的羧酸类似,戊酸带有难闻的气味,类似于臭袜子的气味。它存在于多年生
  • 乔凡尼·多美尼科·卡西尼乔凡尼·多美尼科·卡西尼(意大利文:Giovanni Domenico Cassini,1625年6月8日-1712年9月14日),法文名让-多米尼克·卡西尼(Gian Domenico Cassini或Jean-Dominique Cassini),是一位在
  • 台币新台币,是中华民国现行的法定货币,于1949年6月15日起发行流通,当时定位为限定台湾使用的区域货币,目前则在整个中华民国有效统治区域(台澎金马)均可流通使用。原始发行机构为台湾
  • 光化学烟雾光化学烟雾(Photochemical smog)指的是一系列对环境和健康有害的化学品。称之为光化学烟雾是因为它们是由氮氧化物及挥发性有机物等污染物质光解而产生的,之后会留下悬浮粒子及
  • 经济学院北京大学经济学院是隶属于北京大学的一个学院,正式成立于1985年。北大经济学院是北京大学历史最悠久的院系之一,也是中国高校中最早建立的专门经济系科。1909年京师大学堂正式
  • 陈道明陈道明(1955年4月26日-),生于天津,祖籍浙江绍兴,现居北京市,中国大陆男演员,毕业于中央戏剧学院。陈道明早年就读天津市第十二中学(现为天津市华光中学)和常德道小学。其父亲是天津市
  • 禁区禁区(英文:Restricted Area),是指一个“未经许可不允许进入”的特殊地区或者区域,也可以说受邀请的、指定人士才可进人该区。禁区有多种,例如机场禁区、边境禁区、休战区、禁渔区
  • 莱奥·本哈克莱奥·本哈克(Leo Beenhakker,1942年8月2日-)生于荷兰的鹿特丹,是国际著名的足球教练。任教的著名球队包括阿贾克斯、费耶诺德、皇家马德里及萨拉戈萨等,亦曾出任沙特阿拉伯和荷兰
  • 霍恩洛厄家族霍恩洛厄家族是德意志贵族家庭,来自法兰克尼亚地区,此家族在1450年成为伯爵,后又升格为亲王国;最终在1806年成立的莱茵邦联中分割并入巴伐利亚、符腾堡两个王国。在霍恩洛厄被兼