clarification between design patterns

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

clarification between design patterns

Post by raghavan20 »

in structural patterns, there are
1. adapter pattern
2. facade pattern


both these patterns appear to be similar

1. as i understand from adapter pattern, we are developing a new interface for a client as the client is not happy with the interface provided by functionality providing class
2. facade is, when there is a huge set of classes and you develop a set of interfaces so that you can easily avail services provided by those classes.

Can anybody explain me when you would choose one of these patterns for a problem?
User avatar
dbevfat
Forum Contributor
Posts: 126
Joined: Tue Jun 28, 2005 2:47 pm
Location: Ljubljana, Slovenia

Post by dbevfat »

Adapter adapts one implementation to another, facade hides complexity behind a specific interface.

This is not a perfect example, but here it is. Imagine you have your own class for sending e-mails:

Code: Select all

class Mailer
{
  function send($target, $subject, $body);
}
And you decide to switch to a third party library, which has the following interface:

Code: Select all

class OtherMailer
{
  function setTarget($target);
  function setSubject($subject);
  function setBody($body);
  function send();
}
Your code has $mailer = new Mailer(); and $mailer->send(...) all over the place. Now you turn your Mailer class into an adapter for OtherMailer. This step (refactoring your class) is no part of the adapter at all, but here it is, for the sake of this example:

Code: Select all

class Mailer
{
  function send($target, $subject, $body)
  {
    $mailer = new OtherMailer();
    $mailer->setTarget($target);
    $mailer->setSubject($subject);
    $mailer->setSubject($body);
    $mailer->send();
  }
}
This class is now an Adapter for the OtherMailer. The main goal here is to adapt a known (and used) class to another class with a different interface.

Now for the Facade pattern. Imagine a complex xml writer:

Code: Select all

class ComplexXmlWriter
{
  function open($doctype);
  function writeElement($element);
  function close();
}

class ComplexXmlElement
{
  ...
}
If you have to write a specific xml (say to write your User class to an xml), you don't want to clutter your code with assembling the complex xml, so you hide the logic behind a facade:

Code: Select all

class User
{
  var $name;
  var $email;
}

class UserXmlWriter
{
  function write($user)
  {
    $writer = new ComplexXmlWriter();
    $writer->open('...');
    $writer->writeElement(new ComplexXmlElement('name', $user->name));
    $writer->writeElement(new ComplexXmlElement('email', $user->email));
    $writer->close();
  }
}
This class is a facade in terms that it uses the more complex library available to present an easy interface to a specific solution.

The examples are not the best, both can be improved, but they do their job, I hope.

Best regards
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Those are good examples, dbevfat, well, they worked for me at least. Thanks :)
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

Jenk wrote:Those are good examples, dbevfat, well, they worked for me at least. Thanks :)
That is correct. Thanks dbevfat.

So basically, adapter just develops a new interface to the service-providing class so that existing code can still work without any modification

facade is used when you think directly communicating with service-providing class is complex and you do some pre-processing before you forward to service-providing class.

as you guys know understanding design patterns is difficult and it is mainly because there is not much freely available good documentation on design patterns. There is a website being partly run by our community member which talks about PHP design patterns. the website is good but not written in a way that could be easily understood, you guys can think of sharing your knowledge there. I still use wikipedia's material on design patterns as reference.
User avatar
theFool
Forum Newbie
Posts: 17
Joined: Thu Oct 26, 2006 2:00 am
Location: Berlin, DE

Post by theFool »

I can recommend the book "Heads First- Design Patterns'.
It explains the basic Design Patterns very well. But the used language is Java there.
Anyway it is one of the few books I can recommend without a doubt.
User avatar
sweatje
Forum Contributor
Posts: 277
Joined: Wed Jun 29, 2005 10:04 pm
Location: Iowa, USA

Post by sweatje »

I believe the key difference between the two is the Adapter targets one class, while Facade targets an entire subsystem (with potentially many classes, factories and builders). The goal of both is to provide your code with a more palatable API than the original API of the code you are Adapting/Facading.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

sweatje wrote:I believe the key difference between the two is the Adapter targets one class, while Facade targets an entire subsystem (with potentially many classes, factories and builders). The goal of both is to provide your code with a more palatable API than the original API of the code you are Adapting/Facading.
that is another good definition.
and theFool(i dont really want to call you that) :wink: , i am looking at the book you have asked me to.
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

Feel free to PM suggestions on the PFP website - I've let it stagnate since September while I'm busy on other projects, but I will be getting back to it before long...;). One of our thoughts (AC too) is to have an overview page before digging into the detail - make it easier to stick a toe in to test the water before running off to the pages long tutorial that follows.
User avatar
sweatje
Forum Contributor
Posts: 277
Joined: Wed Jun 29, 2005 10:04 pm
Location: Iowa, USA

Post by sweatje »

Maugrim_The_Reaper wrote:Feel free to PM suggestions on the PFP website - I've let it stagnate since September while I'm busy on other projects, but I will be getting back to it before long...;). One of our thoughts (AC too) is to have an overview page before digging into the detail - make it easier to stick a toe in to test the water before running off to the pages long tutorial that follows.
My favorite design pattern jump page is The Sacred Elements of the Faith :)
Post Reply