1 / 24

Patterns – Day 4

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.

reinert
Download Presentation

Patterns – Day 4

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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) Creational Structural Behavioral Abstract Factory Adapter Chain of Responsibility Builder Bridge Command Factory Composite Interpreter Prototype Decorator Iterator Singleton Facade Mediator Flyweight Memento Proxy Observer State Strategy Template Visitor

  4. Metsker Design Pattern Organization Interface Responsibility Construction Operation Extension Template Decorator Singleton Builder Adapter Facade Observer Factory State Iterator Composite Mediator Abstract Factory Strategy Visitor Bridge Proxy Prototype Command Flyweight Memento Interpreter Chain of Responsibility

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

  6. RocketSim interface

  7. Interfaces imply responsibility?

  8. Alternative approach

  9. Solution to previous exercise

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

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

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

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

  14. Define a JawtList class • Uses JList • But has awt.List methods publicinterfaceawtList{ publicvoidadd(Strings); publicvoidremove(Strings); publicString[]getSelectedItems(); publicvoidclear(); }

  15. Here is most of such a class //this is a simple adapter class to //convert List awt methods to Swing methods publicclassJawtListextendsJScrollPane implementsListSelectionListener,awtList{ privateJListlistWindow; privateJListDatalistContents; publicJawtList(introws){ listContents=newJListData(); listWindow=newJList(listContents); listWindow.setPrototypeCellValue("Abcdefg Hijkmnop"); getViewport().add(listWindow); } //----------------------------------------- publicvoidadd(Strings){ listContents.addElement(s); } //----------------------------------------- publicvoidremove(Strings){ listContents.removeElement(s); } //----------------- publicvoidclear(){ listContents.clear(); }

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

  17. Here is how we used it kidList=newJawtList(20); //=== privatevoid loadList(Vector v) { kidList.clear(); Iterator iter = v.iterator(); while(iter.hasNext()){ Swimmer sw = (Swimmer) iter.next(); kidList.add(sw.getName()); } }

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

  19. 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 adaptedAdapter (TextShape) adapts the interface of Adaptee to the Target interface.

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

More Related