详细请见:https://www.runoob.com/design-pattern/facade-pattern.html

什么是外观模式???

外观模式隐藏系统的复杂性,并向客户端提供一个客户端可以访问系统的接口。这种类型的设计模式属于结构型模式,它向现有的系统添加一个接口,来隐藏系统的复杂性

这种模式涉及到一个单一的类,该类提供了客户端请求的简化方法和对现有系统类方法的委托调用。

主要解决的问题

  • 降低客户端与复杂子系统之间的耦合度。
  • 简化客户端对复杂系统的操作,隐藏内部实现细节。

缺优点

  • 优点
  1. 减少依赖:客户端与子系统之间的依赖减少。
  2. 提高灵活性:子系统的内部变化不会影响客户端。
  3. 增强安全性:隐藏了子系统的内部实现,只暴露必要的操作。
  • 缺点
  1. 违反开闭原则:对子系统的修改可能需要对外观类进行相应的修改。

代码实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public interface Shape
{
void Draw();
}
public class Rectangle : Shape
{
public void Draw()
{
Debug.Log("Rectangle Draw");
}
}

public class Circle : Shape
{
public void Draw()
{
Debug.Log("Circle Draw");
}
}

public class Square : Shape
{
public void Draw()
{
Debug.Log("Square Draw");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
public class ShapeMaker
{
private Square square = new Square();
private Circle circle = new Circle();
private Rectangle rectangle = new Rectangle();

public void DrawShape()
{
square.Draw();
circle.Draw();
rectangle.Draw();
}
}
1
2
3
4
5
6
private void Start()
{
ShapeMaker shapeMaker = new ShapeMaker();
shapeMaker.DrawShape();

}