外观模式

✍ dations ◷ 2025-09-18 03:23:12 #软件设计模式

外观模式(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();	}}


相关

  • 生长激素生长激素(HGH)是一种肽类激素。它可以促进动物和人的发育以及细胞的增殖。它是一种一百九十一单链肽,含有191个氨基酸分子,由垂体中的生长激素细胞合成、存储和分泌。通过重组DN
  • 心理心理哲学一词有歧义,可能指:
  • 阿维亚诺空军基地阿维亚诺空军基地(意大利语:Base aerea di Aviano;IATA代码:AVB;ICAO代码:LIPA)是美国空军在意大利北部的一个军事基地。该基地位于阿维亚诺,距离波代诺内15公里。现驻扎部队为美国
  • 少数族群迫害反犹骚乱 (Pogrom)是以屠杀及逼害特定民族或宗教群体为目标而发起的暴乱或骚乱,由群众自发或受到政府秘密支持,甚至有时由政府公开鼓吹煽动。该辞汇主要是指历史上在东欧及中
  • 汤姆·普莱斯托马斯·埃德蒙兹·“汤姆”·普莱斯(英语:Thomas Edmunds "Tom" Price;1954年10月8日-),是美国的一位政治人物。共和党党籍。曾任美国卫生与公众服务部长。他因滥用包机花费巨大
  • 马拉梅斯特凡·马拉梅(法语:Stéphane Mallarmé,1842年3月18日-1898年9月9日)原名艾提安·马拉梅(法语:Étienne Mallarmé),19世纪法国诗人,文学评论家。与阿蒂尔·兰波、保尔·魏尔伦同为
  • 127<< 120121122123124125126127128129>> 127是126与128之间的自然数。
  • 瑜伽师地论龙树、圣天、无著、 世亲、陈那、法称、 释迦光、功德光 【其他】─ 《入中论》 《释量论》 《俱舍论》 《现观庄严论》 《戒律本论》【其他】─ 《瑜伽师地论》(梵语:Yogāc
  • 各国固定电话数目列表只列出首100名国家。
  • 生态箱生态箱是一类模拟自然生态环境的动植物饲养场所,通常是封闭或半封闭式的,一般上生态箱的设计会设法让里面的动植物保持一种半自供自给的生态平衡。依据生态箱的制作材料及培养