patterns – day 4 interfaces and adapter reminders: faculty candidate talks monday and thursday. no...

24
Patterns – Day 4 Interfaces and Adapter Reminders: Faculty candidate talks Monday and Thursday. No class on Monday.

Post on 20-Dec-2015

216 views

Category:

Documents


2 download

TRANSCRIPT

Patterns – Day 4

Interfaces and Adapter

Reminders: Faculty candidate talks Monday and Thursday.No class on Monday.

Quotes from James Cooper

The [patterns] field has developed its own jargon.

Some writing on this subject has been a bit obscure.

Learning about Design Patterns entitles you to join an elite fraternity with its own language.

GoF Design Pattern Organization (slide by James Cooper)

Creational Structural BehavioralAbstract Factory Adapter Chain of

ResponsibilityBuilder Bridge CommandFactory Composite InterpreterPrototype Decorator IteratorSingleton Facade Mediator

Flyweight MementoProxy Observer

StateStrategyTemplateVisitor

Metsker Design Pattern Organization

Interface Responsibility Construction

Abstract Factory

Adapter

Chain of Responsibility

Builder

Bridge Command

FactoryComposite

InterpreterPrototype

DecoratorIterator

SingletonFacade

Mediator

Flyweight MementoProxy

Observer StateStrategy

Template

Visitor

Operation Extension

Interfaces

Why do you think Metsker included this chapter that’s basically just about Java interfaces?

RocketSim interface

Interfaces imply responsibility?

Alternative approach

Solution to previous exercise

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

• Also known as Wrapper.

• The next five slides are from Jim Cooper’s tutorial at OOPSLA 2002. Used with permission.

Let’s consider the Java awt.List and JList

• 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 usepublic JList(ListModel dataModel) ;

• Everything else takes place in the data model.

Define a JawtList class

• Uses 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 List awt methods to Swing methods

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

• Is WindowAdapter an example of the Adapter pattern?

More on Adapter Tuesday.