Download presentation
Presentation is loading. Please wait.
1
Patterns – Day 6 Adapter continued Reminders: Faculty candidate talk today 4:20 PM O-167. No class next Tuesday. Course newsgroup: rhit.cs.patterns
2
Questions, comments, rebuttals, etc.?
3
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.
4
Oozinoz example This is a “Class adapter” public interface RocketSim { abstract Length apogee(); public Force thrust(); }
5
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.Figure 3.2 This ShellAsRocket class relies on fireworks physics that can model a shell's apogee and "thrust" from its muzzle velocity.
6
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.Figure 3.2 This ShellAsRocket class relies on fireworks physics that can model a shell's apogee and "thrust" from its muzzle velocity.
7
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.GUIgraphical user interface To display data from your domain, JTable provides constructors that accept an instance of the TableModel interface.
8
The AbstractTableModel class provides defaults for all but a few of the methods in TableModel.
9
CHALLENGE 3.2 CHALLENGE 3.2 Fill in the code for the RocketTable methods that adapt an array of Rocket objects to serve as a TableModel.
10
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.3CHALLENGE 3.3 Write down a comment explaining what might go wrong with this approach to adaptation. CHALLENGE 3.4CHALLENGE 3.4 You'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?
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.