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

Post on 22-Dec-2015

213 views 0 download

transcript

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.

Comments or questions?

• On things from last time

• or anything else?

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.

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

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

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.

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

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

}

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

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

}

}

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.

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

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.

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.

Figure 3.3

Some code on the next slides should make this clearer.

Dimension class

DimensionConstants interface

The Measure class

Individual Measure classes

UnitConstants interface

Back to the big picture …

Figure 3.3

Unit conversion examples

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

Evaluation

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