外观模式将复杂系统的内部子系统与客户程序之间的依赖解除,它侧重于简化接口,更多的是一种架构模式。
外观模式角色:
◊Facade:将客户端的请求转给相应的子系统处理。
◊ SubSystem:实现各个子系统的具体功能。
优点:
1、实现子系统与客户端之间松耦合关系
2、减少客户端处理对象的数目,使子系统使用其他更加方便容易。
结构图:
namespace Libraries.Test.DesignPattern.Facade
{
public class Computer
{
private Cpu cpu;
private Memory memory;
private HardDisk hardDisk;
private MainBoard mainBoard;
public Computer() {
cpu = new Cpu();
memory = new Memory();
hardDisk = new HardDisk();
mainBoard = new MainBoard();
}
/// <summary>
/// 标配电脑
/// </summary>
/// <returns></returns>
public string GetStandardComputer()
{
return cpu.GetStandardCpu() + memory.GetMemory() + hardDisk.GetStandardHardDisk() + mainBoard.GetStandardMainBoard();
}
/// <summary>
/// 高配电脑
/// </summary>
/// <returns></returns>
public string GetHighComputer()
{
return cpu.GetHighCpu() + memory.GetMemory() + hardDisk.GetHighHardDisk() + mainBoard.GetHighMainBoard() + graphics.GetGraphics();
}
}
}
namespace Libraries.Test.DesignPattern.Facade
{
public class Cpu
{
public string GetStandardCpu()
{
return "英特尔(Intel) i5;";
}
public string GetHighCpu()
{
return "英特尔(Intel) i7;";
}
}
}
namespace Libraries.Test.DesignPattern.Facade
{
public class HardDisk
{
public string GetStandardHardDisk()
{
return "机械硬盘;";
}
public string GetHighHardDisk()
{
return "固态硬盘;";
}
}
}
namespace Libraries.Test.DesignPattern.Facade
{
public class MainBoard
{
public string GetStandardMainBoard()
{
return "华硕TUF B350M-PLUS;";
}
public string GetHighMainBoard()
{
return "华硕ROG STRIX Z370-E;";
}
}
}
namespace Libraries.Test.DesignPattern.Facade
{
public class Memory
{
public string GetMemory()
{
return "金士顿 8G;";
}
}
}
namespace Libraries.Test.DesignPattern.Facade
{
public class Graphics
{
public string GetGraphics()
{
return "七彩虹显卡;";
}
}
}
namespace Libraries.Test.DesignPattern.Facade
{
public class Client
{
public void Excute()
{
Computer computer = new Computer();
//标配电脑
string standardComputer = computer.GetStandardComputer();
//高配电脑
string highComputer = computer.GetHighComputer();
}
}
}