常用外观模式的学习
外观模式(Facade Pattern)隐藏系统的复杂性,并向客户端提供了一个客户端可以访问系统的接口。这种类型的设计模式属于结构型模式,它向现有的系统添加一个接口,来隐藏系统的复杂性。
原理:为子系统中的一组接口提供一个一致的界面,外观模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。
这里作者认为最重要的一点是分工明确;
主要解决:降低访问复杂系统的内部子系统时的复杂度,简化客户端与之的接口。
何时使用:
1、客户端不需要知道系统内部的复杂联系,整个系统只需提供一个"接待员"即可。
2、定义系统的入口。
如何解决:客户端不与系统耦合,外观类与系统耦合。
基本实现:
假如现在校长需要各个班的一个期末考试的总结,但是他如果直接去找班长和团支书肯定是不现实的,因为神马都是亲力亲为的话,那么他得找班长、团支书、副班长、学习委员等等一系列,就很杂乱。那么他必然会去找老师做这个工作,老师再以班为单位找到各个班委。收取总结,再上交到学院。这样一来,一个人只是做一件具体的事。
这里就很清晰的体现了面向对象的基本原理,分而治之
//==========================
// - FileName: President.cs
// - Created: true.
// - CreateTime: 2020/03/05 15:04:48
// - Email: 1670328571@qq.com
// - Region: China WUHAN
// - Description: 校长
//==========================
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class President
{
public Teacher teacher = new Teacher();
public void OrderTeacher()
{
teacher.OrderStu();
}
}
//==========================
// - FileName: Teacher.cs
// - Created: true.
// - CreateTime: 2020/03/05 15:05:35
// - Email: 1670328571@qq.com
// - Region: China WUHAN
// - Description: 老师类
//==========================
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Teacher
{
private Monitor monitor = new Monitor();
private Leaguesecretary leaguesecretary = new Leaguesecretary();
public void OrderStu()
{
monitor.WriteSummary();
leaguesecretary.WriteSummary();
}
}
//==========================
// - FileName: Monitor.cs
// - Created: true.
// - CreateTime: 2020/03/05 15:06:22
// - Email: 1670328571@qq.com
// - Region: China WUHAN
// - Description: 班长
//==========================
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Monitor
{
public void WriteSummary()
{
Debug.Log("班长的总结");
}
}
//==========================
// - FileName: Leaguesecretary.cs
// - Created: true.
// - CreateTime: 2020/03/05 15:07:10
// - Email: 1670328571@qq.com
// - Region: China WUHAN
// - Description:
//==========================
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Leaguesecretary
{
public void WriteSummary()
{
Debug.Log("团支书的总结");
}
}
//==========================
// - FileName: DPFacade.cs
// - Created: true.
// - CreateTime: 2020/03/05 15:03:56
// - Email: 1670328571@qq.com
// - Region: China WUHAN
// - Description: 外观模式测试
//==========================
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DPFacade : MonoBehaviour
{
private void Start()
{
President pres = new President();
pres.OrderTeacher();
}
}
所以这里就实现了外观模式。