当前位置:Gxlcms > PHP教程 > 外观模式的作用

外观模式的作用

时间:2021-07-01 10:21:17 帮助过:65人阅读

外观模式(门面模式)

外观模式是指通过外观的包装,使应用程序只能看到外观对象,而不会看到具体的细节对象,这样无疑会降低应用程序的复杂度,并且提高了程序的可维护性。

门面模式的优点

1、它对客户屏蔽了子系统组件,因而减少了客户处理的对象的数目并使得子系统使用起来更加方便

2、实现了子系统与客户之间的松耦合关系

3、如果应用需要,它并不限制它们使用子系统类。因此可以在系统易用性与能用性之间加以选择

门面模式适用场景

1、为一些复杂的子系统提供一组接口

2、提高子系统的独立性

3、在层次化结构中,可以使用门面模式定义系统的每一层的接口

比如我们在网站开发中有以下几个功能

关闭和开启网站、关闭开启博客、关闭开启注册3个功能我们可以将他们实现后并包装起来。

  1. <?php
  2. //关闭和开启网站
  3. class webSet{
  4. public function start(){
  5. echo '开启网站......';
  6. }
  7. public function stop(){
  8. echo '关闭网站......';
  9. }
  10. }
  11. //关闭开启博客
  12. class blogSet{
  13. public function start(){
  14. echo '开启博客......';
  15. }
  16. public function stop(){
  17. echo '关闭博客......';
  18. }
  19. }
  20. //关闭开启注册
  21. class registerSet{
  22. public function start(){
  23. echo '开启注册......';
  24. }
  25. public function stop(){
  26. echo '关闭注册......';
  27. }
  28. }
  29. //门面类
  30. class Facade{
  31. //网站设置对象
  32. private $webSet;
  33. //博客设置对象
  34. private $blogSet;
  35. //注册功能设置对象
  36. private $registerSet;
  37. public function __construct(){
  38. $this->webSet = new webSet();
  39. $this->blogSet = new blogSet();
  40. $this->registerSet = new registerSet();
  41. }
  42. //设置共开关 - 开
  43. public function turnOn(){
  44. $this->webSet->start();
  45. $this->blogSet->start();
  46. $this->registerSet->start();
  47. }
  48. //设置共开关 - 关
  49. public function turnOff(){
  50. $this->webSet->stop();
  51. $this->blogSet->stop();
  52. $this->registerSet->stop();
  53. }
  54. }
  55. //调用
  56. $Facade = new Facade();
  57. $Facade->turnOn();

以上就是外观模式的作用的详细内容,更多请关注Gxlcms其它相关文章!

人气教程排行