100 likes | 221 Views
Patterns – Day 6. Adapter continued Reminders: Faculty candidate talk today 4:20 PM O-167. No class next Tuesday. Course newsgroup: rhit.cs.patterns. Questions, comments, rebuttals, etc.?. Adapter Pattern. GoF Definition:
E N D
Patterns – Day 6 Adapter continued Reminders: Faculty candidate talk today 4:20 PM O-167. No class next Tuesday. Course newsgroup: rhit.cs.patterns
Adapter Pattern GoF Definition: • Intent: Convert the interface of a class into another interface that clients expect. • Adapter lets classes work together that otherwise couldn’t because of incompatible interface. • Also known as Wrapper. Metsker Definition: • provide the services the client expects, using the services of a class that has a different interface.
Oozinoz example This is a “Class adapter” public interface RocketSim { abstract Length apogee(); public Force thrust(); }
CHALLENGE 3.1 • Complete the class diagram in Figure 3.2 to show the design of a ShellAsRocket class that lets a Shell object participate in a simulation as a RocketSim object. This ShellAsRocket class relies on fireworks physics that can model a shell's apogee and "thrust" from its muzzle velocity.
CHALLENGE 3.1 - solution • Complete the class diagram in Figure 3.2 to show the design of a ShellAsRocket class that lets a Shell object participate in a simulation as a RocketSim object. This ShellAsRocket class relies on fireworks physics that can model a shell's apogee and "thrust" from its muzzle velocity.
Object Adapter example • The previous example is a class adapter, since ShellAsRocket extends Shell. • Now we look at Metsker’s object adapter example. • A class that requires you to write an object adapter is the JTable class in javax.swing. • This class creates a GUI (graphical user interface) table component filled with the information that your adapter feeds to it. • To display data from your domain, JTable provides constructors that accept an instance of the TableModel interface.
The AbstractTableModel class provides defaults for all but a few of the methods in TableModel.
CHALLENGE 3.2Fill in the code for the RocketTable methods that adapt an array of Rocket objects to serve as a TableModel.
Unforseen Adaptation • Adaptation is easy when the client plans for it. • In Metsker’s example, we have a situation where we can redirect “standard” output to a PrintStream that is different than System.out. • What if we wish to redirect to a JTextArea? CHALLENGE 3.4You'd like to narrow the responsibility of the MessageAdapter class with a specification of the calls the client might make to it. What request should you make? CHALLENGE 3.3Write down a comment explaining what might go wrong with this approach to adaptation.