patterns – day 5 adapter reminders: faculty candidate talk thursday. no class next tuesday. course...

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

Post on 22-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

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

Patterns – Day 5

Adapter

Reminders: Faculty candidate talk Thursday.

No class next Tuesday.

Course newsgroup: rhit.cs.patterns

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

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.

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

Comments or questions?

• On things from last time

• or anything else?

Page 4: Patterns – Day 5 Adapter Reminders: Faculty candidate talk Thursday. 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.

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

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

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

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) ;

public void clear() ;

public void remove(int position) ;

public String[] getSelectedItems() ;

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

JList is an improvement

• Better looking

• More flexible

• But much harder to use

public JList(ListModel dataModel) ;

• Everything else takes place in the data model.

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

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();}

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

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();

}

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

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

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

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());

}

}

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

GoF ExampleA 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.

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

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

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

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.

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

Sidebar- Oozinoz units of measure

• Should we care about fireworks?• A measure is a combination of a magnitude and

a dimension. • A dimension is an aggregation of a measure's

length, mass, and time exponents. – For example, a volume, such as one ft3, is three

dimensional in length. – An acceleration of 9.8 meters/second2 has a length

dimension of 1 and a time dimension of –2. – The units package models commonly used

dimensions, as Figure 3.3 shows. – Note that the subclasses of Measure are dimensions,

such as Length and Force, not units, such as inches and pounds.

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

Figure 3.3

Some code on the next slides should make this clearer.

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

Dimension class

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

DimensionConstants interface

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

The Measure class

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

Individual Measure classes

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

UnitConstants interface

Back to the big picture …

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

Figure 3.3

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

Unit conversion examples

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

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

Evaluation

• Pros and cons of this system of units and measures?