外观模式(Facade Pattern)

本文介绍了软件设计中的外观模式,通过简化子系统的接口使系统更易于使用。以学生选课系统为例,展示了如何通过创建一个外观类来整合多个子系统的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

模式定义

外观模式(Facade Pattern):外部与一个子系统的通信必须通过一个统一的外观对象进行,为子系统中的一组接口提供一个一致的界面,外观模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。
其实就是专门定义一个类关联子系统,处理各个子系统的调用逻辑。

UML类图

1236909-20180130183546625-1847910086.png

  • Facade 外观角色 在客户端直接调用的角色,在外观角色中可以知道相关的(一个或者多个)子系统的功能和责任,
    它将所有从客户端发来的请求委派到相应的子系统去,传递给相应的子系统对象处理;实现:关联子系统角色,方便调用子系统的功能
  • SubSystem 子系统角色 具体子系统

    代码结构

public static class FacadeApp
    {
        public static void Run()
        {
            Facade facade = new Facade();
            facade.Method();
        }
    }

    public class SubSystemOne
    {
        public void MethodOne()
        {
            Console.WriteLine("SubSystemOne MethodOne");
        }
    }

    public class SubSystemTwo
    {
        public void MethodTwo()
        {
            Console.WriteLine("SubSystemTwo MethodTwo");
        }
    }

    public class SubSystemThree
    {
        public void MethodThree()
        {
            Console.WriteLine("SubSystemThree MethodThree");
        }
    }

    public class Facade
    {
        private SubSystemOne _one;
        private SubSystemTwo _two;
        private SubSystemThree _three;

        public Facade()
        {
            _one = new SubSystemOne();
            _two = new SubSystemTwo();
            _three = new SubSystemThree();
        }

        public void Method()
        {
            Console.WriteLine("\nMethodA() ----- ");
            _one.MethodOne();
            _two.MethodTwo();
            _three.MethodThree();
        }
    }

情景模式

这里引用Learning Hard博客中的例子,这个例子太好的,上过学的都明白。学生选课

/// <summary>
    /// 以学生选课系统为例子演示外观模式的使用
    /// 学生选课模块包括功能有:
    /// 验证选课的人数是否已满
    /// 通知用户课程选择成功与否
    /// 客户端代码
    /// </summary>
    class Student
    {
        private static RegistrationFacade facade = new RegistrationFacade();

        static void Main(string[] args)
        {
            if (facade.RegisterCourse("设计模式", "Learning Hard"))
            {
                Console.WriteLine("选课成功");
            }
            else
            {
                Console.WriteLine("选课失败");
            }

            Console.Read();
        }
    }

    // 外观类
    public class RegistrationFacade
    {
        private RegisterCourse registerCourse;
        private NotifyStudent notifyStu;
        public RegistrationFacade()
        {
            registerCourse = new RegisterCourse();
            notifyStu = new NotifyStudent();
        }

        public bool RegisterCourse(string courseName, string studentName)
        {
            if (!registerCourse.CheckAvailable(courseName))
            {
                return false;
            }

            return notifyStu.Notify(studentName);
        }
    }

    #region 子系统
    // 相当于子系统A
    public class RegisterCourse
    {
        public bool CheckAvailable(string courseName)
        {
            Console.WriteLine("正在验证课程 {0}是否人数已满", courseName);
            return true;
        }
    }

    // 相当于子系统B
    public class NotifyStudent
    {
        public bool Notify(string studentName)
        {
            Console.WriteLine("正在向{0}发生通知", studentName);
            return true;
        }
    }
    #endregion

转载于:https://www.cnblogs.com/LoveTomato/p/8386548.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值