1 / 26

Understanding Design Patterns of Inheritance and Adapter in Java

Dive into the concepts of adapter design pattern versus inheritance design pattern in Java, exploring the efficient implementation of stacks using linked lists. Learn about adapting existing classes, implementing interfaces, and employing composition design patterns for efficient stack operations in Java.

ruizpascual
Télécharger la présentation

Understanding Design Patterns of Inheritance and Adapter in Java

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. CS2 in Java Peer Instruction Materials by Cynthia Lee is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License. Based on a work at http://peerinstruction4cs.org. Permissions beyond the scope of this license may be available at http://peerinstruction4cs.org. CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12

  2. 2 Today’s Topics 1. Adapter design pattern vs inheritance design pattern 2. Stacks

  3. Reading quiz!

  4. Reading quiz!  We define the new class C, declared to implement interface I, to include an instance of type A (composition design pattern).  QUESTION:  A private instance variable from A can be accessed directly and used to represent the corresponding variable in C A. TRUE B. FALSE

  5. Reading Quiz!  A stack is a Last-In-First-Out (LIFO) data structure. Your task is to implement a Stack directly by using a singly linked list (a class that implements List<E> only).  QUESTION:  Is it more efficient to use (A) the end /or/ (B) the front of the linked list as the top of the stack?

  6. Design Patterns

  7. Inheritance Design Pattern Problem: We need to create a class C that implements interface I We know of an existing class B that offers some of the functionality required by I And this functionality in B corresponds exactly to a subset of the API of I Solution: define the new class C to extend the existing class B C “is a” B      public class C extends B implements I { …

  8. Adapter Design Pattern Problem: We need to create a class C that implements interface I  Identify an existing class A that has attributes and behavior similar to those required by I  A is not an exact subset of I’s requirements Define a class named, say, C, declared to implement I, which includes an instance variable of type A  C “has a” A public class C implements I { private A myA; …

  9. Adapter Design Pattern So each instance of C includes an instance of A: every C has-a A  This is called containment or composition The methods in C are defined to call methods in A to do (some of) their work  This is called message forwarding or delegation

  10. Stack Using ArrayList

  11. What attribute of List corresponds to Stack.top?  What is the time cost of adding or removing an element at the head or at the tail of an N- element List… If List is implemented using an array? Head: ________ Tail: _________  O(1), O(1) O(1), O(n) O(n), O(n) O(n2), O(n2) Other/none/more A. B. C. D. E.

  12. What attribute of List corresponds to Stack.top?  What is the time cost of adding or removing an element at the head or at the tail of an N- element List… If List is implemented using a singly linked list? Head: ________ Tail: _________  O(1), O(1) O(1), O(n) O(n), O(n) O(n2), O(n2) Other/none/more A. B. C. D. E.

  13. What attribute of List corresponds to Stack.top?  What is the time cost of adding or removing an element at the head or at the tail of an N- element List… If List is implemented using a doubly linked list with tail pointer? Head: ________ Tail: _________  O(1), O(1) O(1), O(n) O(n), O(n) O(n2), O(n2) Other/none/more A. B. C. D. E.

  14. Map Stack Attributes to ArrayList Attributes and/or Methods StackAttribute ArrayList Equivalent size() – 1 top size() size Don’t underestimate the importance of doing this mapping first. Planning now saves time later

  15. Map Stack Methods to List Methods  A consequence of that attribute mapping is that a push operation results in adding to the tail of the List size() – 1 + 1 = size() tail of list next position beyond tail location to “push” the new stack element Stack operation List operation equivalent push( element ) add( size(), element ) E pop() E remove( size() - 1 ) E peek() E get( size() – 1 ) int size() int size() boolean isEmpty() boolean isEmpty()

  16. What does the implementation look like? 1 2 3 import java.util.ArrayList; 4 import java.util.List; 5 import java.util.EmptyStackException; 6 7 /** 8 * An implementation of the Stack interface that adapts 9 * a java.util.List 10 */ 11 public class ListStack<E> implements Stack<E> { 12 private java.util.List<E> stack; 13 // the top element of stack is stored at position 14 // s.size() - 1 in the list. 15 16 /** 17 * Create an empty stack. 18 */ 19 public ListStack() { 20 stack = new ArrayList<E>(); 21 } 22 The contained instance of the adapted class

  17. What does the implementation look like? 23 /** 24 * Determine if the stack is empty. 25 * @return <tt>true</tt> if the stack is empty, 26 * otherwise return <tt>false</tt>. 27 */ 28 public boolean isEmpty() 29 { 30 return stack.isEmpty(); 31 } 32 33 /** 34 * Return the top element of the stack without removing it. 35 * This operation does not modify the stack. 36 * @return topmost element of the stack. 37 * @throws EmptyStackException if the stack is empty. 38 */ 39 public E peek() 40 { 41 if ( stack.isEmpty() ) 42 throw new EmptyStackException(); 43 return stack.get( stack.size() - 1 ) ; 44 } message forwarding – let the List object do as much work as possible Having done the attribute mapping, this is easy to figure out

  18. What does the implementation look like? 46 /** 47 * Pop the top element from the stack and return it. 48 * @return topmost element of the stack. 49 * @throws EmptyStackException if the stack is empty. 50 */ 51 public E pop() 52 { 53 if ( stack.isEmpty() ) 54 throw new EmptyStackException(); 55 return stack.remove( stack.size() - 1 ); 56 } 57 58 /** 59 * Push <tt>element</tt> on top of the stack. 60 * @param element the element to be pushed on the stack. 61 */ 62 public void push( E element) 63 { 64 stack.add( stack.size(), element ); 65 } Compare with peek() List.size() -1 is top, so “push” new element at List.size()

  19. Stack Using LinkedList

  20. Single or Double?  To implement the Stack<E> interface with linked list, we should use: A. Singly-linked list with head pointer only Singly-linked list with head + tail pointers C. Doubly-linked list with head pointer only D. Doubly-linked list with head+ tail pointers Other/none/more than one B. E.

  21. To implement Stack<E> interface with linked list, we should use:  Singly-linked list with head pointer only  We will use the head of the list as the top of the stack  All operations can happen there directly  Stack only needs to access one end

  22. Map Stack ADT attributes to a linked list implementation public class LinkedStack<E> implements Stack<E> { private int size; private SLNode<E> top; LinkedStack LinkedStack size top 0 SLNode element successor

  23. Map Stack ADT attributes to a linked list implementation public class LinkedStack<E> implements Stack<E> { private int size; private SLNode<E> top;  This Stack with Linked List is using the Adapter design pattern, because it contains a link list object rather than inherits one by extending a linked list. A. TRUE B. FALSE LinkedStack LinkedStack size 0 top SLNode element successor

  24. The push() operation 1. create a new SLNode (a) 1 public void push( E element ) { 2 3 SLNode newNode = 4 new SLNode<E>(element, 5 top.getSuccessor()); (a, b) 6 7 top.setSuccessor( newNode ); 8 9 size++; (d) 10 } 2. Set the new node’s successor field to be the same as top’s next field (b) (c) 3. Set top’s successor field to reference the new node. (c) 4. Increment size by 1 (d) Pushing an element onto an empty stack Steps (a) and (b) Step (c)

  25. 08-25/32 The push() operation 1. create a new SLNode (a) 1 public void push( E element ) { 2 3 SLNode newNode = 4 new SLNode<E>(element, 5 top.getSuccessor()); (a, b) 6 7 top.setSuccessor( newNode ); 8 9 size++; (d) 10 } 2. Set the new node’s successor field to be the same as top’s next field (b) (c) 3. Set top’s successor field to reference the new node. (c) 4. Increment size by 1 (d) Pushing an element onto a non-empty stack Steps (a) and (b) Step (c)

  26. Stack Operation Costs Stack Operation ArrayList Operation Array List Cost push( element ) add( size(), element ) Ο(1) pop() remove( size() – 1 ) Ο(1) peek() get( size() – 1 ) Ο(1) What is the cost of Stack operations for the direct implementation using a singly linked list? A. push=O(1), pop=O(1), peek=O(1) B. push=O(1), pop=O(n), peek=O(n) C. push=O(n), pop=O(1), peek=O(1) D. push=O(n), pop=O(n), peek=O(n) E. Other/none/more

More Related