Design patterns V : Facade Pattern

/*

Author: Jiangong SUN

*/


Facade Pattern (外观模式) provides a unique interface to a set of methods of different subsystems in an application. It's widely used in application development.

It's a structural pattern.


Now let's see the implementation in CSharp.

Firstly, create subsystems and their methods.

        class SubSytemOne
        {
            public void MethodOne()
            {
                Console.WriteLine("SubSystemOne Method");
            }
        }
        class SubSytemTwo
        {
            public void MethodTwo()
            {
                Console.WriteLine("SubSystemTwo Method");
            }
        }
        class SubSytemThree
        {
            public void MethodThree()
            {
                Console.WriteLine("SubSystemThree Method");
            }
        }
        class SubSytemFour
        {
            public void MethodFour()
            {
                Console.WriteLine("SubSystemThree Method");
            }
        }

Create Facade class and its methods as an interface to subsystems' methods' aggregation.

        class Facade
        {
            private readonly SubSytemOne _one;
            private readonly SubSytemTwo _two;
            private readonly SubSytemThree _three;
            private readonly SubSytemFour _four;
            public Facade()
            {
                _one = new SubSytemOne();
                _two = new SubSytemTwo();
                _three = new SubSytemThree();
                _four = new SubSytemFour();
            }
            public void MethodA()
            {
                Console.WriteLine("MethodA():");
                _one.MethodOne();
                _three.MethodThree();
            }
            public void MethodB()
            {
                Console.WriteLine("MethodB():");
                _two.MethodTwo();
                _four.MethodFour();
            }
            public void MethodC()
            {
                Console.WriteLine("MethodC():");
                _one.MethodOne();
                _two.MethodTwo();
                _four.MethodFour();
            }
        }

The clients can call facade's methods to satisfy its requirements.

                var facade = new Facade();
                facade.MethodA();
                facade.MethodB();
                facade.MethodC();

So this is the implementation of facade pattern. I hope you enjoy this article. Enjoy coding!



references:

http://www.dofactory.com/Patterns/PatternFacade.aspx#_self1

http://zh.wikipedia.org/wiki/%E5%A4%96%E8%A7%80%E6%A8%A1%E5%BC%8F

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值