在Unity新建一个脚本,在这里我取名为DPFacade,并复制以下的代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 外观模式的内容(上下级之间的调用)
/// </summary>
public class DPFacade : MonoBehaviour
{
void Start()
{
Principal pc = new Principal();
pc.OrderTeacherToDoTask();
}
}
/// <summary>
/// 上层管理
/// </summary>
public class Principal
{
private Teacher teacher = new Teacher();
/// <summary>
/// 校长给老师发消息
/// </summary>
public void OrderTeacherToDoTask()
{
teacher.OrderStudentsToSummary();
}
}
/// <summary>
/// 外观角色
/// </summary>
public class Teacher
{
private Monitor monitor = new Monitor();
private LeagueSecretary leagueSecretary = new LeagueSecretary();
/// <summary>
/// 老师给班上学生发消息
/// </summary>
public void OrderStudentsToSummary()
{
monitor.WriteSummary();
leagueSecretary.WriteSummary();
}
}
/// <summary>
/// 班长学生类
/// </summary>
public class Monitor
{
public void WriteSummary()
{
Debug.Log("班长的总结");
}
}
/// <summary>
/// 团支书学生类
/// </summary>
public class LeagueSecretary
{
public void WriteSummary()
{
Debug.Log("团支书的总结");
}
}