Chapter 6. Fitting Round Pegs into Square Holes with the Adapter and Facade Patterns

<feature> <title>In This Chapter</title> <objective>

Using the Adapter pattern

</objective>
<objective>

Creating adapters

</objective>
<objective>

Adapting Ace objects as Acme objects

</objective>
<objective>

Handling adapter issues

</objective>
<objective>

Using the Facade pattern

</objective>
</feature>

Sometimes, objects just don’t fit together as they should. A class may have changed, or an object turns out to be just too difficult to work with. This chapter comes to the rescue by covering two design patterns: the Adapter pattern and the Facade pattern. The Adapter design pattern lets you adapt what an object or class has to offer so that another object or class can make use of it. The Facade design pattern is similar, in that it changes the look of an object, but the goal here is a little different: You use this design pattern to simplify the exposed methods of an object or class, making it easier to work with that object or class.

The Adapter Scenario

“Alright,” says the MegaGigaCo team leader, entering the room, “hold everything. Management has decreed that we switch our back-end framework to the one sold by the CEO’s nephew’s company.”

“Hmm,” says a programmer, “that could be a problem. Our online user interface takes customer orders using software from the Ace company and packages them in objects of the Ace class. What type of objects can we pass to the new back end?”

“Only the new Acme objects,” the ...

Get Design Patterns For Dummies® now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.