Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Patterns – Day 4 Interfaces and Adapter Reminders: Faculty candidate talks Monday and Thursday. No class on Monday."— Presentation transcript:

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

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

3 GoF Design Pattern Organization (slide by James Cooper) CreationalStructuralBehavioral Abstract FactoryAdapterChain of Responsibility BuilderBridgeCommand FactoryCompositeInterpreter PrototypeDecoratorIterator SingletonFacadeMediator FlyweightMemento ProxyObserver State Strategy Template Visitor

4 Metsker Design Pattern Organization InterfaceResponsibilityConstruction Abstract Factory Adapter Chain of Responsibility Builder BridgeCommand Factory Composite Interpreter Prototype Decorator Iterator Singleton Facade Mediator FlyweightMemento Proxy ObserverState Strategy Template Visitor OperationExtension

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

6 RocketSim interface

7

8 Interfaces imply responsibility?

9

10

11

12 Alternative approach

13 Solution to previous exercise

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

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

16 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) ;String public void clear() ; public void remove(int position) ; public String[] getSelectedItems() ;String

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

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

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

20 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

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

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

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

24 Is WindowAdapter an example of the Adapter pattern? More on Adapter Tuesday.


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

Similar presentations


Ads by Google