Presentation is loading. Please wait.

Presentation is loading. Please wait.

Patterns – Day 5 Adapter Reminders: Faculty candidate talk Thursday. No class next Tuesday. Course newsgroup: rhit.cs.patterns.

Similar presentations


Presentation on theme: "Patterns – Day 5 Adapter Reminders: Faculty candidate talk Thursday. No class next Tuesday. Course newsgroup: rhit.cs.patterns."— Presentation transcript:

1 Patterns – Day 5 Adapter Reminders: Faculty candidate talk Thursday. No class next Tuesday. Course newsgroup: rhit.cs.patterns

2 Results of informal survey Friday’s discussion was NOT too much low-level detail. We should look at specific examples in Java; we can understand things better when it is made concrete. We should talk about the challenge problems in detail. We should not rush through the first patterns that we study.

3 Comments or questions? On things from last time or anything else?

4 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.

5 The next few slides are from Jim Cooper’s tutorial at OOPSLA 2002. Used with permission. Reminder: Cooper’s book is available to us via Safari OnLine.

6 Let’s consider the Java awt.List and JList classes The awt.List is easier to use –But not very good looking –Hardly light weight – public List(int rows) ; public void add(String item) ;String public void clear() ; public void remove(int position) ; public String[] getSelectedItems() ;String

7 JList is an improvement Better looking More flexible But much harder to use public JList(ListModel dataModel) ;JListListModel Everything else takes place in the data model.

8 Define a JawtList class Uses a JList But has awt.List methods public interface awtList { public void add(String s); public void remove(String s); public String[] getSelectedItems(); public void clear(); }

9 Here is most of such a class //this is a simple adapter class to //convert Swing methods to an AWT interface public class JawtList extends JScrollPane implements ListSelectionListener, awtList { private JList listWindow; private JListData listContents; public JawtList(int rows) { listContents = new JListData(); listWindow = new JList(listContents); listWindow.setPrototypeCellValue("Abcdefg Hijkmnop"); getViewport().add(listWindow); } //----------------------------------------- public void add(String s) { listContents.addElement(s); } //----------------------------------------- public void remove(String s) { listContents.removeElement(s); } //----------------- public void clear() { listContents.clear(); }

10 This is an Adapter pattern An adapter class converts the interface of one class to another. There are two ways to do this –Derive a new class from old one and add new methods (inheritance) –Create a class which contains an instance of old class and passes method calls to it. (object containment) These are called –Class adapters, and –Object adapters

11 Here is how we used it kidList = new JawtList(20); //=== private void loadList(Vector v) { kidList.clear(); Iterator iter = v.iterator(); while(iter.hasNext()){ Swimmer sw = (Swimmer) iter.next(); kidList.add(sw.getName()); }

12 GoF Example A Graphics toolkit may have a number of Shape objects that can be manipulated in a certain way (BoundingBox, CreateManipulator). There is no TextShape class, but there is TextView, which provides the needed functionality, but not the expected interface.

13 GoF general situation In Java, Target would probably be an interface. Target (Shape) defines the interface that client uses. Client (DrawingEditor) collaborates with objects conforming to the Target interface. Adaptee (TextView) defines an existing interface that needs to be adapted Adapter (TextShape) adapts the interface of Adaptee to the Target interface. and delegation

14 Figure 3.1. When a developer of client code thoughtfully defines the client's needs, your mission is to adapt the interface to an existing class.

15 Sidebar- Oozinoz units of measure Should we care about fireworks? A measure is a combination of a magnitude and a dimension.measure A dimension is an aggregation of a measure's length, mass, and time exponents.dimension –For example, a volume, such as one ft 3, is three dimensional in length. –An acceleration of 9.8 meters/second 2 has a length dimension of 1 and a time dimension of –2. –The units package models commonly used dimensions, as Figure 3.3 shows.Figure 3.3 –Note that the subclasses of Measure are dimensions, such as Length and Force, not units, such as inches and pounds.

16 Figure 3.3 Some code on the next slides should make this clearer.

17 Dimension class

18 DimensionConstants interface

19 The Measure class

20 Individual Measure classes

21 UnitConstants interface Back to the big picture …

22 Figure 3.3

23 Unit conversion examples Output: I see: 0.75 gallon(s), 173.2 cubic inch(es), 2839.0 milliliter(s).

24 Evaluation Pros and cons of this system of units and measures?


Download ppt "Patterns – Day 5 Adapter Reminders: Faculty candidate talk Thursday. No class next Tuesday. Course newsgroup: rhit.cs.patterns."

Similar presentations


Ads by Google