您的位置:首页 > 其它

设计模式-外观模式(facade pattern)

2015-03-27 14:27 357 查看
名称: 外观模式

说说:电脑启动需要经历哪些环节我不知道,我只需要按下开机按钮

动机:

适用性:

参与者:

结果:将一套行为封装到一个方法中,即定义一个高层接口,用来访问子系统中的一群接口

类图:



说明:简化了客户与子系统的交互过程,降低了客户与子系统的耦合度

demo c#:

namespace facade {
    class Program {
        static void Main(string[] args) {
            var _tv = new tv();
            var _light = new light();
            var _other = new otherEquipment();
            var power = new Power(_tv, _light, _other);
            power.powerOff();

            Console.Read();
        }
    }

    class tv {
        public void powerOff() {
            Console.WriteLine("tv is turn off");
        }
    }
    class light {
        public void lightOff() {
            Console.WriteLine("light is turn off");
        }
    }
    class otherEquipment {
        public void allClose() {
            Console.WriteLine("all Electric equipment closed!");
        }
    }

    class Power {
        tv _t;
        light _l;
        otherEquipment _o;
        public Power(tv t, light l, otherEquipment o) { 
            this._t = t;
            this._l = l;
            this._o = o;
        }
        // 统一的高层接口,控制一群子系统对象
        public void powerOff(){
            this._t.powerOff();
            this._l.lightOff();
            this._o.allClose();
        } 
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: