Course Hero Logo

Stackstring stk new stackstring stkpushstuff as a

Course Hero uses AI to attempt to automatically extract content from documents to surface to you and others so you can study better, e.g., in search results, to enrich docs, and more. This preview shows page 22 - 28 out of 31 pages.

Stack<String> stk =new Stack<String>();stk.push(“stuff”);...}As a Front for Different ImplementaVons(Plug and Play)22
02/14/19CS 213 Spring '19 - Sesh VenugopalThe client needs to make appropriate changesin the code in order to use the LL alternaVve:package util;public class LLStack<T> {private Node<T> items;public LLStack() {...}public void llpush(T t) {...}...}package apps;import util.*;public class SomeApp {...LLStack<String> stk =new LLStack<String>();stk.llpush(“stuff”);...}Theutilgroup wants to provide an alternaVvestack implementaVon that uses a linked listinstead of anArrayList.In the process, it changes the name of thepush method:To switch between alternaVves, client has to make several changes.FuncVonality (WHAT can be done -­‐ push) bleeds into implementaVon(HOW it can be done – ArrayList/Linked List) in the push/llpush methods.(Plug and Play)23
02/14/19CS 213 Spring '19 - Sesh VenugopalStack AlternaVves: Bemer soluVonStack interfacepackage util;public interface Stack<T> {void push(T t);T pop();...}24package util;public class ALStack<T>implements Stack<T> {private ArrayList<T> items;public ALStack() {...}public void push(T t) {...}public T pop() {...}...}ArrayListversionpackage util;public class LLStack<T>implements Stack<T> {private Node<T> items;public LLStack() {...}public void push(T t) {...}public T pop() {...}...}Linked List version
02/14/19CS 213 Spring '19 - Sesh VenugopalStack AlternaVves: Bemer soluVonStack clientpackage apps;public class SomeApp {...Stack<String> stk =new ALStack<String>();stk.push(“stuff”);...}package apps;public class SomeApp {...Stack<String> stk =new LLStack<String>();stk.push(“stuff”);...}To use other stack, onlyone change – innew25Use interfaceStackfor staVc type
02/14/19CS 213 Spring '19 - Sesh VenugopalPlug and Play – Example 2List list = new ArrayList( );. . .list.<List method>(. . .). . .ArrayList list = new ArrayList( );. . .list.<ArrayList method>(…). . .ArrayListused, staVcally typed toArrayList:ArrayListused, staVcally typed toList(interface)In an applicaVon that does stuff with lists, there is a choice of what kind of list to use:26OR
02/14/19CS 213 Spring '19 - Sesh Venugopal

Upload your study docs or become a

Course Hero member to access this document

Upload your study docs or become a

Course Hero member to access this document

End of preview. Want to read all 31 pages?

Upload your study docs or become a

Course Hero member to access this document

Term
Spring
Professor
rajivg
Tags
Sesh Venugopal, public class Searcher

Newly uploaded documents

Show More

Newly uploaded documents

Show More

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture