外观模式

目录

一、外观模式

二、外观模式角色

三、代码实例一

四、代码实例二


一、外观模式

外观模式也称门面模式,外部和子系统之间必须以一个统一的外观对象进行。

本质是封装交互、简化调用。

使用场景:

1、为一个复杂系统提供一个接口

2、客户程序与抽象类的实现有很大依赖

3、层次化结构中一个层次提供一个外观作为入口点

4、希望包装或隐藏原有系统

它的优点很显然:

1、减少了系统之间的相互依赖

2、提高了灵活性和安全性

3、屏蔽了外部客户端和系统内部的交互

4、实现了复用,可被多个客户端调用

但是外观模式不符合开闭原则(对修改开关闭,对扩展开放)。

符合迪米特法则,即最少知识法则。
迪米特法则:如果两个类不必彼此直接通信,那么这两个类就不应当发生直接的相互作用。如果其中一个类需要调用另一个类的某一个方法的话,可以通过第三者转发这个调用。

二、外观模式角色

1、外观角色

客户端通过调用该角色方法,来完成功能。

2、子系统角色

可以有多个子系统。

三、代码实例一

购买股票和基金

子系统类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 外观模式
{
    class Stock1
    {
        public void Sell()
        {
            Console.WriteLine("股票一卖出");
        }
        public void Buy()
        {
            Console.WriteLine("股票一买入");
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 外观模式
{
    class Stock2
    {
        public void Sell()
        {
            Console.WriteLine("股票二卖出");
        }
        public void Buy()
        {
            Console.WriteLine("股票二买入");
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 外观模式
{
    class NationalDebt1
    {
        public void Sell()
        {
            Console.WriteLine("国债一卖出");
        }
        public void Buy()
        {
            Console.WriteLine("国债一买入");
        }
    }
}

外观角色:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 外观模式
{
    class Fund
    {
        Stock1 s1;
        Stock2 s2;
        NationalDebt1 n1;
        public Fund()
        {
            s1 = new Stock1();
            s2 = new Stock2();
            n1 = new NationalDebt1();
        }
        public void Buy()
        {
            s1.Buy();
            s2.Buy();
            n1.Buy();
        }
        public void Sell()
        {
            s1.Sell();
            s2.Sell();
            n1.Sell();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 外观模式
{
    class Program
    {
        static void Main(string[] args)
        {
            Fund f = new Fund();
            f.Buy();
            f.Sell();
            //Console.Read();
        }
    }
}

四、代码实例二

秘书帮老板安排

子系统类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FacadeOne
{
    class Airport
    {
        public void BookTicket(string from,string to)
        {
            Console.WriteLine("订购了从"+from+"到"+to+"的机票");
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FacadeOne
{
    class Hotel
    {
        public void Reserve(int num)
        {
            Console.WriteLine("订了"+num+"天的房间");
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FacadeOne
{
    class Restaurant
    {
        public void Reserve(string name,int num)
        {
            Console.WriteLine("订了" + name + "酒店的" + num + "桌");
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FacadeOne
{
    class Secretary
    {
        private Hotel hotel = new Hotel();
        private Restaurant restaurant = new Restaurant();
        Airport airport = new Airport();
        public void Arrange(string to,string where,int num)
        {
            hotel.Reserve(num);
            restaurant.Reserve(where, 1);
            airport.BookTicket("北京", to);
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FacadeOne
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Secretary man = new Secretary();
            man.Arrange("青岛","御膳房",2);
            //Application.EnableVisualStyles();
            //Application.SetCompatibleTextRenderingDefault(false);
           // Application.Run(new Form1());
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值