原文:https://phpenthusiast.com/blog/simplify-your-php-code-with-facade-class

----------------------------------------------------------------

How to simplify a PHP code with the help of the façade pattern?

Published November 26, 2015

We need to consider the use of the façade pattern in those cases that the code that we want to use consists of too many classes and methods, and all we want is a simple interface, preferably one method, that can do all the job for us.

 
 

The problem: complicated code with too many classes and methods

A real life example can be a code that shares the newest posts in our blog with several social networks. Each social network has its own class, and a set of methods to share our posts.

  • A CodeTwit class to tweet on twitter.
  • A Googlize class to share our posts on Google plus.
  • And a Reddiator class to share in reddit.

That's the code for the three classes:

 1 2 3 4 5 6 7 8 91011121314151617181920212223// Class to tweet on Twitter.
class CodeTwit {
  function tweet($status, $url)
  {
    var_dump('Tweeted:'.$status.' from:'.$url);
  }
} // Class to share on Google plus.
class Googlize {
  function share($url)
  {
    var_dump('Shared on Google plus:'.$url);
  }
} // Class to share in Reddit.
class Reddiator {
  function reddit($url, $title)
  {
    var_dump('Reddit! url:'.$url.' title:'.$title);
  }
}

The problem is that every time that we want to share our posts, we need to call to all of the methods. It's too much work for us! We want to simplify the system, and instead of calling to all the methods, call to only one method.

The solution: a Façade class

We can simplify the code by using a Façade class with the following characteristics:

  1. It holds references to the classes that it uses (in our case, to the CodeTwit, Googlize, and the Reddiatorclasses).
  2. It has a method that calls all of the methods that we need.

A façade class enables us to call only one method instead of calling to many methods. By doing so, it simplifies the work with the system, and allows us to have a simpler and more convenient interface.

In our example, the shareFacade class gets the social networks objects injected to its constructor, holds these objects by reference, and has the ability to call to all of the share methods from a single share method.

That's the code:

 1 2 3 4 5 6 7 8 91011121314151617181920212223242526272829303132333435// The Facade class
class shareFacade {
  // Holds a reference to all of the classes.
  protected $twitter;
  protected $google;
  protected $reddit;   // The objects are injected to the constructor.
  function __construct($twitterObj,$gooleObj,$redditObj)
  {
    $this->twitter = $twitterObj;
    $this->google = $gooleObj;
    $this->reddit = $redditObj;
  }   // One function makes all the job of calling all the share methods
  // that belong to all the social networks.
  function share($url,$title,$status)
  {
    $this->twitter->tweet($status, $url);
    $this->google->share($url);
    $this->reddit->reddit($url, $title);
  }
} // Create the objects from the classes.
$twitterObj = new CodetTwit();
$gooleObj = new Googlize();
$redditObj = new Reddiator(); // Pass the objects to the class facade object.
$shareObj = new shareFacade($twitterObj,$gooleObj,$redditObj); // Call only 1 method to share your post with all the social networks.
$shareObj->share('https://myBlog.com/post-awsome','My greatest post','Read my greatest post ever.');

And that's all! We got what we wanted. We shared our post with three social networks by calling only a single sharemethod.

In conclusion

We use the Façade class in those cases that we need to simplify a complex code that has too many classes and too many functions, and all that we need is a simple interface that allows us to work with only a single class, most often, with a single method.

How to simplify a PHP code with the help of the façade pattern?的更多相关文章

  1. python code practice(二):KMP算法、二分搜索的实现、哈希表

    1.替换空格 题目描述:请实现一个函数,将一个字符串中的每个空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. 分析: 将长度为 ...

  2. The CLR's Thread Pool

    We were unable to locate this content in zh-cn. Here is the same content in en-us. .NET The CLR's Th ...

  3. react与jQuery对比,有空的时候再翻译一下

    参考资料:http://reactfordesigners.com/labs/reactjs-introduction-for-people-who-know-just-enough-jquery-t ...

  4. Learning WCF Chapter1 Generating a Service and Client Proxy

    In the previous lab,you created a service and client from scratch without leveraging the tools avail ...

  5. Frontend Development

    原文链接: https://github.com/dypsilon/frontend-dev-bookmarks Frontend Development Looking for something ...

  6. FFmpeg资料来源简单分析:libswscale的sws_getContext()

    ===================================================== FFmpeg库函数的源代码的分析文章: [骨架] FFmpeg源码结构图 - 解码 FFmp ...

  7. react programming

    So you're curious in learning this new thing called Reactive Programming, particularly its variant c ...

  8. FFmpeg源代码简单分析:libswscale的sws_getContext()

    ===================================================== FFmpeg的库函数源代码分析文章列表: [架构图] FFmpeg源代码结构图 - 解码 F ...

  9. CSCI 1100 — Computer Science 1 Homework

    CSCI 1100 — Computer Science 1 Homework 8CS1 Multiverse: ClassesOverviewThis homework is worth 100 p ...

随机推荐

  1. selenium与360极速浏览器driver配置

    1)下载浏览器对应的driver,浏览器版本与driver对应关系,网址:http://www.cnblogs.com/JHblogs/p/7699951.html:driver下载地址:http:/ ...

  2. Ubuntu下查看CPU、内存和硬盘详细信息的几个命令

    CPU: 型号:grep "model name" /proc/cpuinfo |awk -F ':' '{print $NF}' 数量:lscpu |grep "CPU ...

  3. 【SQL】服务器环境下的SQL

    一.大型数据库的三层体系结构 web服务器:比如在淘宝页面上,输入“牛肉干”,就是web服务器来处理,提交给应用服务器. 应用服务器:在获取到“牛肉干”这个请求后,应用服务器决定如何汇集结果,并进行相 ...

  4. redis持久化的方法及对比

    1.持久化的作用 redis所有的数据保持在内存中,对数据的更新将异步的保存到磁盘上. 两种方式: 2.RDB 2.1.概念 2.2.触发机制 2.2.1.save 同步 因为是同步命令,数据量大的话 ...

  5. Visual Studio Code更改语言

    参数地址:Visual Studio Code 设置Display Language介绍 在Visual Studio Code中使用快捷键Ctrl + Shift + P可以打开命令行 在local ...

  6. 【转】Celery 分布式任务队列快速入门

    Celery 分布式任务队列快速入门 本节内容 Celery介绍和基本使用 在项目中如何使用celery 启用多个workers Celery 分布式 Celery 定时任务 与django结合 通过 ...

  7. django web 自定义通用权限控制

    需求:web系统有包含以下5个url,分别对于不同资源: 1.stu/add_stu/ 2.stu/upload_homework/ 3.stu/query_homework/ 4.stu/add_r ...

  8. selenium 难定位元素,时间插件,下拉框定位,string包含,定位列表中的一个,技巧

    关于frame: 如果网页存在iframe的话,传统的定位有时候找不到元素,需要切换frame: # 切换到leftFrame定位“测井设计” driver.switch_to_frame(" ...

  9. 二维字符数组利用gets()函数输入

    举例: ][]; ;i<;i++) gets(a[i]); a是二维字符数组的数组名,相当于一维数组的指针, 所以a[i]就相当于指向第i个数组的指针,类型就相当于char *,相当于字符串.

  10. (14)python 文件和流

    打开文件 f=open('C:\Temp.txt') 读取数据 f.read(); 关闭文件 f.close();#关闭后将无法再读取 打开文件的方式 不写模式,默认是只读模式 1.r 打开只读文件, ...