zoukankan      html  css  js  c++  java
  • 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.

    • CodeTwit class to tweet on twitter.
    • 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.

  • 相关阅读:
    文本框只能填数字
    Win7 IIS 配置错误:不能在此路径中使用此配置节。如果在父级别上锁定了该节,便会出现这种情况。锁定是默认设置的
    C#中小数点后保留两位小数,四舍五入的函数及使用方法(转)
    数据库中where与having区别
    NET Runtime version 2.0.50727.42
    C盘文件过大,C盘空间莫名丢失,pagefile.sys文件
    VS2008水晶报表变两页(重装系统后)
    win8 IIS配置
    从程序员到项目经理(5):程序员加油站 -- 懂电脑更要懂人脑
    从程序员到项目经理(4):程序员加油站 -- 不是人人都懂的学习要点
  • 原文地址:https://www.cnblogs.com/oxspirt/p/9262748.html
Copyright © 2011-2022 走看看