1 / 10

Patterns – Day 6

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:

allene
Download Presentation

Patterns – Day 6

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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. 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. 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. • 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.2Fill 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.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.

More Related