How to simplify a PHP code with the help of the façade pattern?
原文: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:
- It holds references to the classes that it uses (in our case, to the CodeTwit, Googlize, and the Reddiatorclasses).
- 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?的更多相关文章
- python code practice(二):KMP算法、二分搜索的实现、哈希表
1.替换空格 题目描述:请实现一个函数,将一个字符串中的每个空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. 分析: 将长度为 ...
- 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 ...
- react与jQuery对比,有空的时候再翻译一下
参考资料:http://reactfordesigners.com/labs/reactjs-introduction-for-people-who-know-just-enough-jquery-t ...
- 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 ...
- Frontend Development
原文链接: https://github.com/dypsilon/frontend-dev-bookmarks Frontend Development Looking for something ...
- FFmpeg资料来源简单分析:libswscale的sws_getContext()
===================================================== FFmpeg库函数的源代码的分析文章: [骨架] FFmpeg源码结构图 - 解码 FFmp ...
- react programming
So you're curious in learning this new thing called Reactive Programming, particularly its variant c ...
- FFmpeg源代码简单分析:libswscale的sws_getContext()
===================================================== FFmpeg的库函数源代码分析文章列表: [架构图] FFmpeg源代码结构图 - 解码 F ...
- CSCI 1100 — Computer Science 1 Homework
CSCI 1100 — Computer Science 1 Homework 8CS1 Multiverse: ClassesOverviewThis homework is worth 100 p ...
随机推荐
- UVA 10471 Gift Exchanging
题意:就5种盒子,给出每个盒子个数,盒子总数,每个人选择这个盒子的概率.求这个人选择哪个盒子取得第一个朋友的概率最大,最大多少 dp[N][sta]表示当前第N个人面临状态sta(选择盒子的状态可以用 ...
- Jmeter4.0启动闪退问题解决方案
jmeter:4.0 jdk版本:1.8 在Jmeter.bat的最后添加pause可以让Jmeter启动停止: 添加了pause进行强制停止在启动命令页面,查看到Jmeter报错信息如下: 第一次解 ...
- 利用Java编写简单的WebService实例-转载
使用Axis编写WebService比较简单,就我的理解,WebService的实现代码和编写Java代码其实没有什么区别,主要是将哪些Java类发布为WebService.下面是一个从编写测试例子到 ...
- cmake 同时编译多个cpp文件
cmake_minimum_required(VERSION 2.8) project(coding) file(GLOB_RECURSE EXTRA_FILES */*) add_custom_ta ...
- iPhone手机关闭ios10自动更新
在手机上打开safari,地址栏输入:https://oldcat.me/web/NOOTA9.mobileconfig然后回车按照要求安装此provision文件即可,然后重启.桌面出现一个反馈感叹 ...
- HDU2923 Einbahnstrasse (Floyd)
Einbahnstrasse Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- [P2397] yyy loves Maths VI (mode)
Link: P2397 传送门 Solution: (1)在这里记录一个小小的黑科技:摩尔投票法 (线性时间复杂度,$O(1)$的空间复杂度求众数) 从数组的第一个元素开始,假定它代表的群体的人数是最 ...
- BZOJ 1529 [POI2005]ska Piggy banks(并查集)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1529 [题目大意] 给出一张n个点n条边的有向图,问选取几个点为起点可以遍历全图 [题 ...
- UVA 1514 Piece it together (二分图匹配)
[题目链接] Link [题目大意] 给你一些由一块黑块和两块白块组成的L形拼图,问你是否能够拼成给出的图 [题解] 我们将所有的黑块拆点,拆分为纵向和横向,和周围的白块连边, 如果能够得到完美匹配, ...
- 【块状树】bzoj3720 Gty的妹子树
块状树.教程见:http://z55250825.blog.163.com/blog/static/1502308092014163413858/将树按一定大小分块,分成许多子树,在每个子树的根节点记 ...