Practice-Design Patterns PDF
Practice-Design Patterns PDF
1. T or F? Coupling and cohesion are closely linked in that as one increases, so does the other.
a. True
b. False
2. Given classes A and B, which of the following is not a common type of coupling in object-oriented
software?
d. A invokes methods of B
2. e
3. c
4. b
Problem: Match the design pattern to the situation to which you should apply it.
RoboChef( ElectroluxGasOven(
1(
…( Econtrols( …(
…( +heat(howHot:(Temperature,(
((((((((((((howLong:(Time)(
…(
Note that the Electrolux Company provided the software interface for controlling the gas oven (Electro-
luxGasOven), and you created the intelligent decision-making part (RoboChef). As your next step, you
would like your system to support different types of ovens other than Electolux gas ones. For example,
Maytag and Whirlpool each provide their own software interfaces for their ovens:
MaytagMicrowave- WhirlpoolElectricRange-
…- …-
+cook(seconds:-Integer,- +bake(degrees:-Integer,-
------------power:-Integer)- ------------@me:-Time)-
…- …-
Update your current software design to allow easy switching between oven-control systems. Your design
must apply the adapter pattern.
IntelligentPilot* CessnaControls*
1*
…* 7controls* …*
…* +doBarrelRoll()*
…*
Note that the Cessna Aircraft Company provided the software interface for controlling the plane (Cess-‐
naControls), and you created the intelligent decision-making part (IntelligentPilot).
As your next step, you would like your system to support different types of airplanes other than Cessnas.
For example, Boeing and Airbus each provide their own software control interfaces:
BoeingCockpitControl. AirbusCtrl.
…. ….
+barrelRollNow(). +engageBarrelRoll().
…. ….
Update your current software design to allow easy switching between control systems. Your design must
apply the adapter pattern.
What effect did your new design have on the coupling between class IntelligentPilot and class Cess-‐
naControls.
PostscriptPrinter)
Document)
printer) …)
…)
*) printPS(doc):)PostScriptDoc))
…)
…)
However, there are other types of printers that a document might want to print itself on, but these printers
have slightly different interfaces than the Postscript printer, for example:
LinePrinter( ThreeDPrinter(
…( …(
doPrint(doc(:(TextData)( renderImage(img(:(Model3D)(
…( …(
Using the adapter pattern, refactor the design, so that the different types of printers can be easily
swapped in and out.