Design Patterns - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

Design Patterns

Description:

Adapters enable the client and the adaptee to be completely decoupled from each other ... Java code sample */ TARGET. interface Stack { void push (Object) ... – PowerPoint PPT presentation

Number of Views:17
Avg rating:3.0/5.0
Slides: 14
Provided by: usersC1
Category:
Tags: design | enable | java | patterns

less

Transcript and Presenter's Notes

Title: Design Patterns


1
Design Patterns
  • Section 7.1 (JIAs)
  • Section 7.2.1 (till page 259) (JIAs)
  • Section 7.2.2(JIAs)
  • Section 10.4.1 (JIAs)

2
Template Design Pattern
  • What is a design pattern?
  • Useful?
  • Singleton?
  • Template?

3
Adapter Design Pattern
  • Hook up your laptop in Europe
  • Pattern Name Adapter
  • Category Structural design pattern
  • Intent To convert the interface of a class into
    another interface that new clients expect
  • The Adapter pattern is used so that two unrelated
    interfaces can work together
  • The join between them is called an Adapter
  • We convert interface of one class into interface
    expected by the client
  • A.K.A Wrapper
  • Applicability when we need to use an existing
    class with a different interface than desired

4
Structure of Object Adapter Pattern
5
Structure of Object Adapter Pattern
  • Object adapters use delegation to adapt one
    interface to another
  • The adapter implements the target interface that
    the client expects to see, while it holds an
    instance of the adaptee
  • Can extend adaptee instead (limited to a single
    adaptee)
  • Object (composition) vs. Class (inheritance)
    Adapter
  • When the client calls the request() method on its
    target object (the adapter), the request is
    translated into the corresponding specific
    request on the adaptee
  • Adapters enable the client and the adaptee to be
    completely decoupled from each other
  • Only the adapter knows about both of them

6
Structure of Class Adapter Pattern
Adaptee
7
Adapter Design Pattern
  • Participants
  • Client
  • Requires objects conforming to ATarget
  • ATarget interface
  • Defines the interface expected by the Client
  • Adaptee class
  • Defines the undesired interface of an existing
    class
  • Adapter
  • Adapts the interface of Adaptee to ATarget via
    delegation (composition) or inhertince

8
Example Object Adapter
  • CLIENT application would like to use a stack
    interface on DList (i.e. adapt ADAPTEE to TARGET)
  • ADAPTEE
  • / DoubleLinkedList /
  • class DList
  • public void insert (DNode pos, Object o) ...
  • public void remove (DNode pos, Object o) ...
  • public void insertHead (Object o) ...
  • public void insertTail (Object o) ...
  • public Object removeHead () ...
  • public Object removeTail () ...
  • public Object getHead () ...
  • public Object getTail () ...
  • TARGET
  • interface Stack
  • void push (Object)
  • Object pop ()
  • Object top ()
  • ADAPTER
  • / Adapt DList class to Stack interface /
  • class DListStack implements Stack
  • private DList _dlist
  • public DListStack()
  • _dlist new DList()
  • public void push (Object o) _dlist.insertTail
    (o)
  • public Object pop ()
  • return _dlist.removeTail ()
  • public Object top ()
  • return _dlist.getTail ()

9
Example Class Adaptor
  • / Java code sample /
  • TARGET
  • interface Stack
  • void push (Object)
  • Object pop ()
  • Object top ()
  • ADAPTEE
  • /DoubleLinkedList /
  • class DList
  • public void insert (DNode pos, Object o) ...
  • public void remove (DNode pos, Object o) ...
  • public void insertHead (Object o) ...
  • public void insertTail (Object o) ...
  • public Object removeHead () ...
  • public Object removeTail () ...
  • public Object getHead () ...
  • public Object getTail () ...
  • / Adapt DList class to Stack interface /
  • ADAPTER
  • class DListImpStack extends DList implements
    Stack
  • public void push (Object o) insertTail (o)
  • public Object pop ()
  • return removeTail ()
  • public Object top ()
  • return getTail ()
  • Exam Question Given a Target and an Adaptee,
    devise an object or class adapter

10
Example (Object Adapter)
  • Example page 514-530
  • Table (Generic display table)
  • Client
  • Expects entries of type TableEntry
  • ListTableModel
  • TableEntry (row in a table)
  • Target
  • Student (Information on a Student)
  • Adaptee
  • Does not conform to TableEntry
  • StudentEntry2
  • Adapter
  • Adapts Student to TableEntry

11
Example
12
Example (Class Adapter)
  • Table (Generic display table)
  • Client
  • Expects entries of type TableEntry
  • TableEntry (row in a table)
  • Target
  • Student (Information on a Student)
  • Adaptee
  • Does not conform to TableEntry
  • StudentEntry
  • Adapter
  • Adapts Student to TableEntry

13
Example
Write a Comment
User Comments (0)
About PowerShow.com