Suppose you are creating an apartment rental system, where you have three objects
in your repository. One object performs the geocoding with the help of online
geocoding services. Another object locates that place using a map service. Finally,
another service searches all the apartments for sale in that area.
Now you want to create an easier interface over these three so that any future
developer can work with your library instead of studying them all together. The
following picture shows us the code structure before there is a Facade:
Client A
Geo Locator Apartment
Finder
Google
Map
Client B
Chapter 4
[ 93 ]
Here is the code structure after using Facade:
Client A Client B
Facade
Apartment
Finder Geo Locator Google
Map
Now let us take a look at the code:
class ApartmentFinder
{
public function locateApartments($place)
{
//use the web service and locate all apartments suitable
//search name
//now return them all in an array
return $apartmentsArray();
}
}
?>
class GeoLocator
{
public function getLocations($place)
{
//use public geo coding service like yahoo and get the
//lattitude and
//longitude of that place
return array("lat"=>$lattitude, "lng"=>$longitude);
}
}
?>
class GoogleMap
{
Design Patterns
[ 94 ]
public function initialize()
{
//do initialize
}
public function drawLocations($locations /* array */)
{
//locate all the points using Google Map Locator
}
public function dispatch($divid)
{
//draw the map with in a div with the div id
}
}
?>
These are our concrete classes.
Pages:
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112