Presentation is loading. Please wait.

Presentation is loading. Please wait.

March 200491.3913 Ron McFadyen1 Adapter An adapter converts the interface of a class into another interface clients expect. An adapter lets classes work.

Similar presentations


Presentation on theme: "March 200491.3913 Ron McFadyen1 Adapter An adapter converts the interface of a class into another interface clients expect. An adapter lets classes work."— Presentation transcript:

1 March 200491.3913 Ron McFadyen1 Adapter An adapter converts the interface of a class into another interface clients expect. An adapter lets classes work together that couldn't otherwise (due to incompatible interfaces).

2 March 200491.3913 Ron McFadyen2 The client collaborates with objects conforming to the Target interface. Adapter Adapter adapts the interface of the Adaptee to the Target interface. Adaptee defines an existing interface incompatible with the Target interface. Target defines the interface the Client uses.

3 March 200491.3913 Ron McFadyen3 Adapter example Suppose a client uses a stack to manage its objects. empty stack 1 object pushed on 1 2 nd object pushed on 1 2 3 rd object pushed on 1 2 3 3 rd object popped off 1 2 33 2 1 Last one in is the first one out The client uses the stack interface: push(), pop()

4 March 200491.3913 Ron McFadyen4 Adapter example We can use some other data structure, such as a double linked list, as long as we can translate the stack interface into the double linked list interface. We can use an adapter that hides the implementation from the client. The adapter will use a double linked list to manage the objects.

5 March 200491.3913 Ron McFadyen5 Adapter example /* * Java example A double linked list used with an adapter to provide a stack to a client */ interface Stack { public void push (Object); public Object pop (); } The operations: push and pop, needed for a stack are listed but not implemented

6 March 200491.3913 Ron McFadyen6 Adapter example /* Double Linked List */ 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 () {... } } The operations defined for a double linked list

7 March 200491.3913 Ron McFadyen7 Adapter example /* Adapter of Stack to double linked list */ class DListAdapter implements Stack { public void DListAdapter () { DList dlist = new Dlist() } public void push (Object o) { dlist.insertTail (o); } public Object pop () { return dlist.removeTail (); } The operations defined for a stack are translated into operations on a double linked list

8 March 200491.3913 Ron McFadyen8 Adapter example ClientStack DListAdapter DList insert () remove () insertHead () insertTail () removeHead () removeTail () getHead () getTail () push() pop() push() pop() 1

9 March 200491.3913 Ron McFadyen9 Adapter example :DList:Client push() insertTail() pop() ? The client believes it is using a stack. :DListAdapter hides the implementation from the client. :DListAdapter uses a double linked list to manage objects. :DListAdapter

10 March 200491.3913 Ron McFadyen10 Adapter An adapter converts the interface of a class into another interface clients expect. An adapter lets classes work together that couldn't otherwise because of incompatible interfaces. An adapter provides to the client the functionality promised by an interface, without the client having to assume what class is being used to implement that interface.


Download ppt "March 200491.3913 Ron McFadyen1 Adapter An adapter converts the interface of a class into another interface clients expect. An adapter lets classes work."

Similar presentations


Ads by Google