PHP Design Patterns: The FACADE Pattern

May 22, 2005

The first pattern we'll be taking a look at is the "Facade" pattern. (how to pronounce in audio format http://www.m-w.com/cgi-bin/dictionary?book=Dictionary&va=facade&x=0&y=0) Its basic job is to create a simpler interface to a larger, complex system. What we're trying to do is create one entry point to our system for a simplified purpose. Think about an e-commerce website, think about a company that imports alot of goods to sell. They have many stores and many warehouses that store goods, their e-commerce site is their "facade".

Imagine if they didn't have their store front, and you wanted to find out if your item was in stock and how much it was. You would probably call up the closest store to you and ask about the product, they might not know exactly how much stock they have, so they go and check and tell you that they have 0 items in stock but you should try calling the store in the next town, maybe they have some in stock, so you call over there, no luck, finally you find a store 3 towns away but their price is $10 more because they are in a wealthy town and the rent is higher. So you could go through all this trouble OR you could just use their website and when you click on a product it goes out to all the stores near you, gathers inventory data and presents you with your standard price.

The same concepts apply to software development. An example would be Amazon... Amazon.com allows a developer access to certain API's they are allowed to use. You can search for products, maybe check inventory, and some other useful things. Now lets say you're running your company with all this money poured into a fancy web system that manages your products and orders. You like what amazon has done but you cannot afford to rewrite a whole system just for people to interact with on their website. You want an alternative.. you want the FACADE pattern. Using the facade pattern you can create a "Public API" class in your system that contains methods that you want people to have access to, like getPrice(), or getInventory(). In reality your system isn't so simple, in your system you can manage stock, track orders, fullfillment, order allocation, warehousing, etc but imaging giving a user a 400 page API map of your system, besides the security issues it just wouldn't work.

So using the facade pattern you can open up only the parts of your system you want people to use, maintaining a SINGLE point of entry to your application. Lets imagine for a minute that you don't have your Public API class and you're letting users use your api's directly and lets say something happened in the future that required you to change your Pricing class, it couldn't be helped it had to change, well this would break all your existing clients connection to your site. If you had the Public API class in place you could change out anything you wish so long as the getPrice() method of the Public API returned what the client expected. Take a look at the UML Graphs below.



To relate this to PHP consider the JPGraph Library (http://www.aditus.nu/jpgraph/) which is a 3.8 MB package that allows you to display pretty graphs of data on your website. Let's your boss comes to you with a project. He wants you to incorporate a graph on your website that you and your teammates can use to generate a line graph showing hits to your website over a period of time that your developers can dictate. Your teammates also want to use this feature on pages they're coding but can't take the time to learn JPGraph (which can be involved). So its your job to make this feature as easy as possible so you create a Facade class to interact with JPGraphs library of classes. So you create your JPGraphFacade class so your developers can use it like this:
  1.  
  2. $line_graph = new JPGraphFacade();
  3. $line_graph->showLineGraph($data, $timePeriod);


Try downloading JPGraph and see if it could really be that easy to create your own graphs. You just saved your teammates alot of time and headache because you gave them a great interface to only a feature they needed of a much larger library. If your boss decides later on that he wants to add bar charts you can just modify your Facade class and let them use it like this:
  1.  
  2. $bar_graph = new JPGraphFacade();
  3. $bar_graph->showBarGraph($data, $timePeriod);

Comments

RSS feed for comments on this post.

  1. Anonymous says:
    June 4, 2006 @ 07:28 — Reply

  2. blu ray ripper says:
    April 18, 2010 @ 04:03 — Reply

    Comment pending moderation

  3. wedding dress says:
    May 6, 2010 @ 02:01 — Reply

    Comment pending moderation

  4. Sven says:
    May 6, 2010 @ 02:04 — Reply

    Comment pending moderation

  5. cheap loft beds says:
    May 6, 2010 @ 03:19 — Reply

    Comment pending moderation

  6. Satelite Internet says:
    May 6, 2010 @ 19:35 — Reply

    Comment pending moderation

  7. replacement laptop battery says:
    May 6, 2010 @ 20:42 — Reply

    Comment pending moderation

  8. 642-067 says:
    May 19, 2010 @ 00:21 — Reply

    Comment pending moderation

  9. casa pariurilor says:
    May 21, 2010 @ 11:11 — Reply

    Comment pending moderation

  10. virbram five fingers says:
    June 4, 2010 @ 20:25 — Reply

    Comment pending moderation

  11. virbram five fingers says:
    June 4, 2010 @ 20:27 — Reply

    Comment pending moderation

  12. Coach Purse says:
    June 7, 2010 @ 02:06 — Reply

    Comment pending moderation

  13. Stromlieferant says:
    June 7, 2010 @ 05:09 — Reply

    Comment pending moderation

  14. LOuIs VuItToN Damier Graphite Canvas says:
    June 9, 2010 @ 22:05 — Reply

    Comment pending moderation

  15. CT0-101 says:
    June 12, 2010 @ 02:08 — Reply

    Comment pending moderation

  16. vibram five fingers says:
    June 14, 2010 @ 00:31 — Reply

    Comment pending moderation

  17. air force 1 says:
    June 15, 2010 @ 01:10 — Reply

    Comment pending moderation

  18. surpa shoes says:
    June 15, 2010 @ 01:22 — Reply

    Comment pending moderation

  19. asics shoes says:
    June 15, 2010 @ 01:25 — Reply

    Comment pending moderation

  20. groom wedding speech says:
    June 15, 2010 @ 06:01 — Reply

    Comment pending moderation

  21. Louis Vuitton handbags says:
    June 16, 2010 @ 01:49 — Reply

    Comment pending moderation

  22. replica watches says:
    June 21, 2010 @ 02:41 — Reply

    Comment pending moderation

  23. cheap full lace wigs says:
    June 22, 2010 @ 22:25 — Reply

    Comment pending moderation

  24. air max says:
    June 24, 2010 @ 01:54 — Reply

    Comment pending moderation

Leave a Comment

Line and paragraph breaks automatic, HTML allowed: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <code> <em> <i> <strike> <strong>

Comments disabled due to spammers being losers that lead sad lives.