概述
外观模式也是一种结构型的设计模式,其最主要的功能就是简化接口,将复杂的子系统用简单的方法封装起来,使得客户端在可以在不知道具体实现的情况下使用对应的功能,在实际的开发中,用的是非常非常多的,比如各种插件,各种API,都是这个逻辑,将复杂的功能封装起来,提供接口,调用对应的接口就可以实现对应的功能。实现看下面的例子
需求:现在需要办一场演唱会,一个演唱会公司承办,演唱会公式只需要告诉开始和结束,其它复杂的实现由具体的计划实现。
外观模式
internal class Program
{
private static void Main(string[] args)
{
//演出内容初始化
PerformancePlan PP = new PerformancePlan();
PerformanceRehearse PR = new PerformanceRehearse();
OfficialPerformance OP = new OfficialPerformance();
EndOfPerformance EP = new EndOfPerformance();
PerformanceCompany performanceCompany = new PerformanceCompany(PP, PR, OP, EP);
performanceCompany.BegainPerformance();//开始演出
performanceCompany.EndPerformance();//结束演出
}
public class PerformancePlan//演出计划
{
public void Cost() { Console.WriteLine("花费费用XXXXXX元"); }
public void Time() { Console.WriteLine("演出日期:9月9日"); }
public void Theme() { Console.WriteLine("演出主题:XXXXXX音乐会"); }
public void Location() { Console.WriteLine("地点:天津"); }
}
public class PerformanceRehearse//演出排练
{
public void Personnel() { Console.WriteLine("人员:XX,XXX,XXX"); }
public void RehearseTime() { Console.WriteLine("排练时间:9月8日"); }
public void Layout() { Console.WriteLine("会场布置........"); }
}
public class OfficialPerformance//正式演出
{
public void TicketCheck() { Console.WriteLine("检票"); }
public void Performance() { Console.WriteLine("开始演出....."); }
}
public class EndOfPerformance//结束演出
{
public void Departure() { Console.WriteLine("安排离场"); }
public void CleanUp() { Console.WriteLine("收拾场地......"); }
public void Statistics() { Console.WriteLine("统计收支"); }
}
public class PerformanceCompany //演出公司
{
private PerformancePlan performancePlan;
private PerformanceRehearse performanceRehearse;
private OfficialPerformance officialPerformance;
private EndOfPerformance endOfPerformance;
public PerformanceCompany(PerformancePlan PP, PerformanceRehearse PR, OfficialPerformance OP, EndOfPerformance EP)
{
performancePlan = PP;
performanceRehearse = PR;
officialPerformance = OP;
endOfPerformance = EP;
}
public void BegainPerformance()//开始演出
{
performancePlan.Cost();
performancePlan.Theme();
performancePlan.Time();
performancePlan.Location();
performanceRehearse.Personnel();
performanceRehearse.RehearseTime();
performanceRehearse.Layout();
officialPerformance.Performance();
officialPerformance.TicketCheck();
}
public void EndPerformance()//结束演出
{
endOfPerformance.CleanUp();
endOfPerformance.Statistics();
endOfPerformance.Departure();
}
}
}
输出结果:
花费费用XXXXXX元
演出主题:XXXXXX音乐会
演出日期:9月9日
地点:天津
人员:XX,XXX,XXX
排练时间:9月8日
会场布置........
开始演出.....
检票
收拾场地......
统计收支
安排离场