原文: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. Appium===Appium+Python API(转)

    Appium+python自动化8-Appium Python API 前言: Appium Python API全集,不知道哪个大神整理的,这里贴出来分享给大家. 1.contexts contex ...

  2. Linux内核:关于中断你需要知道的【转】

    转自:http://blog.csdn.net/duqi_2009/article/details/38009717 1.中断处理程序与其他内核函数真正的区别在于,中断处理程序是被内核调用来相应中断的 ...

  3. Django基础之视图

    Django的View(视图) 一个视图函数(类),简称视图,是一个简单的Python 函数(类),它接受Web请求并且返回Web响应. 响应可以是一张网页的HTML内容,一个重定向,一个404错误, ...

  4. Android 开发实用方法大全

    1.格式化价格,这个经常在计算费用精度的时候用到 /** * 格式化价格 * * @param argStr 传入价格字符串 * @return */ public static String get ...

  5. 配置虚拟主机 和 打war包

    配置一台虚拟主机?        在[tomcat]/conf/server.xml文件中的<Engine>标签内部添加一个<Host>标签:            <H ...

  6. Web.Config文件配置之限制上传文件大小和时间

    在邮件发送系统或者其他一些传送文件的网站中,用户传送文件的大小是有限制的,因为这样不但可以节省服务器的空间,还可以提高传送文件的速度.下面介绍如何在Web.Config文件中配置限制上传文件大小与时间 ...

  7. ChannelFactory

    通过前几篇的学习,我们简单了解了WCF的服务端-客户端模型,可以建立一个简单的WCF通信程序,并且可以把我们的服务寄宿在IIS中了.我们不禁感叹WCF模型的简单,寥寥数行代码和配置,就可以把通信建立起 ...

  8. CF 816B Karen and Coffee【前缀和/差分】

    To stay woke and attentive during classes, Karen needs some coffee! Karen, a coffee aficionado, want ...

  9. 26、Django实战第26天:配置全局导航

    1.编辑index.html,继承base.html 2.编辑base.html,根据是否登录不同显示 问题:当我们选中一个导航,选中样式并没有成为选中的状态 因此我们编辑base.html对导航中进 ...

  10. 简单工厂模式(Factory)

    设计模式序言:这是开始学习设计模式的第一步,也是相对简单的一个设计模式,简单工厂设计模式. 简单工厂就是将业务进行封装,减少代码之间的耦合度,使代码更易于扩展和维护. 以下是简单工厂设计模式的图解: ...