外观模式

维基百科,自由的百科全书

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

外观模式是一个设计模式 (计算机)中常用的面向对象程序设计. 类似于一个建筑学中的立面. 立面作为一个前端接口来屏蔽更复杂的底层或结构代码。外观模式可以:

  • 通用简化的API屏蔽与更复杂的内部组件和结构, 以提高Software library的可读性和可用性
  • 为更通用的功能提供上下文特定的接口
  • 在广泛更新重构单层系统(Monolithic System)或紧密耦合(tight coupling)的软件系统, 提供一个简化的启动点,更有利于更多的松耦合(loose coupling)代码

当一个系统非常复杂或难以理解时,开发人员通常会使用 facade 设计模式,因为该系统有许多相互依赖的类,或者因为其源代码不可用。Facade模式隐藏了更大系统的复杂性,为客户端提供了一个更简单的接口。通常会涉及到一个wrapper包含客户端所需的一组成员的。这些成员代表 facade 客户端访问系统并隐藏实现细节。

结构[编辑]

Facade
这个外观类为子系统中Packages 1、2、3提供一个共同的对外介面(接口
Clients
客户对象通过一个外观介面读写子系统中各介面的数据资源
Packages
客户可以通过外观介面读取的内部库。

示例[编辑]

Java[编辑]

这是一个抽象的范例。一个客户“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();
	}
}