优点
1、引入外观模式,是客户对子系统的使用变得简单了,减少了与子系统的关联对象,实现了子系统与客户之间
的松耦合关系。
2、只是提供了一个访问子系统的统一入口,并不影响用户直接使用子系统类
3、降低了大型软件系统中的编译依赖性,并简化了系统在不同平台之间的移植过程
缺点
1、不能很好地限制客户使用子系统类,如果对客户访问子系统类做太多的限制则减少了可变性和灵活性
2、在不引入抽象外观类的情况下,增加新的子系统可能需要修改外观类或客户端的源代码,违背了“开闭原则”
本质:封装交互,简化调用。
//外观莫模式
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using System.Configuration;
namespace yjzcjing
{
class Stock1
{
public void Shell()
{
Console.WriteLine("股票1卖出");
}
public void Buy()
{
Console.WriteLine("股票1买入");
}
}
class Stock2
{
public void Shell()
{
Console.WriteLine("股票2卖出");
}
public void Buy()
{
Console.WriteLine("股票2买入");
}
}
class Stock3
{
public void Shell()
{
Console.WriteLine("股票3卖出");
}
public void Buy()
{
Console.WriteLine("股票3买入");
}
}
class NationalDebt1
{
public void Shell()
{
Console.WriteLine("国债卖出");
}
public void Buy()
{
Console.WriteLine("国债买入");
}
}
class Fund//整合所有用到了方法和属性,提供给外界调用。
{
Stock1 gu1;
Stock2 gu2;
Stock3 gu3;
NationalDebt1 nd1;
public Fund()//构造函数,在初始化的时候,将所有方法初始化。
{
gu1 = new Stock1();
gu2 = new Stock2();
gu3 = new Stock3();
nd1 = new NationalDebt1();
}
public void BuyFund()//集体操作购买
{
gu1.Buy();
gu2.Buy();
gu3.Buy();
nd1.Buy();
}
public void SellFund()//集体操作,出售。
{
gu1.Shell();
gu2.Shell();
gu3.Shell();
nd1.Shell();
}
}
class Program
{//客户端调用。
static void Main(string[] args)
{
Fund yjz = new Fund();
yjz.BuyFund();
yjz.SellFund();
Console.Read();
}
}
}