原文: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. 电脑IP总是变的问题

    如题,如何解决该问题? 右键---->个性化---->更改桌面图标---->添加网络图标 右键网络图标----->属性---->更改适配器设置---->右键属性,找 ...

  2. 【python】抄写爬淘宝已买到的宝贝的代码

    教程地址:http://cuiqingcai.com/1076.html 这一篇掌握的不好.虽然代码可以跑,但是里面的很多东西都一知半解.需要有空的时候系统整理. 原代码中的正则表达式已经失效了,我自 ...

  3. 使用Pygame制作微信打飞机游戏PC版

    前一阵子看了一篇文章:青少年如何使用Python开始游戏开发 .看完照葫芦画瓢写了一个,觉得挺好玩儿,相当于简单学了下Pygame库.这篇文章是个12岁小孩儿写的,国外小孩儿真心NB,想我12岁的时候 ...

  4. Qt笔记——入门

    Qt的介绍 跨平台c++图形用户界面应用程序框架 Qt的框架 父类(基类)子类(派生类) 头文件 QApplication应用程序类 Qt头文件没有.h 头文件和类名一样 有且只有一个应用程序类的对象 ...

  5. 【cocos2d-js官方文档】二、资源管理器Assets Manager

    这篇文档将介绍Cocos2d-JS 3.0的一个重量级新特性:资源管理器(仅支持JSB).资源管理器是为游戏运行时的资源热更新而设计的,这里的资源可以是图片,音频甚至游戏脚本本身.使用资源管理器,你将 ...

  6. Codeforces Gym100952 A.Who is the winner? (2015 HIAST Collegiate Programming Contest)

      A. Who is the winner?   time limit per test 1 second memory limit per test 64 megabytes input stan ...

  7. unused import statement android studio 解决方法

    解决方法:“file”-->“invalidate caches/restart” 解决 感谢大神的解答

  8. CF 990B. Micro-World【数组操作/贪心/STL/二分搜索】

    [链接]:CF [题意]:对任意一个数a[i] ,可以对任意 满足 i != j 且 a[i] > a[j] && a[i] <= a[j] +k 的 a[j] 可以被删掉 ...

  9. HDU 1426 Sudoku Killer【DFS 数独】

    自从2006年3月10日至11日的首届数独世界锦标赛以后,数独这项游戏越来越受到人们的喜爱和重视. 据说,在2008北京奥运会上,会将数独列为一个单独的项目进行比赛,冠军将有可能获得的一份巨大的奖品— ...

  10. java.lang.NoSuchMethodError:org.hibernate.SessionFactory.openSession()Lorg/hibernate/classic/Session

    项目配置了spring 4.0.6.RELEASE 和hibernate4.2.0.Final ,出错原因是因为在Spring 3.1开始没有了HibernateDaoSupport类,而我的却Dao ...