Download presentation
Presentation is loading. Please wait.
Published byTabitha Strickland Modified over 8 years ago
1
David Stotts Computer Science Department UNC Chapel Hill
2
Software Engineering (Bridge Design Pattern)
3
The Bridge design pattern is used to separate the abstract user level API from the backend implementation We can write client code to talk to the front end API We can plug in different back end implementations without changing client code
7
//=== interface side ============================== class Stack { // 3. Create an interface/wrapper class that protected StackImp imp; // "hasa" impl object and delegates public Stack( String s ) { // all requsts to it if (s.equals("java")) { imp = new StackImpJava(); // can add more implementation options //} else if (s.equals("list")) { // imp = new StackImpList(); } else { imp = new StackImpMine(); } } public Stack() { this( "java" ); } public void push( int in ) { imp.push( new Integer(in) ); } public int pop() { return ((Integer)imp.pop()).intValue(); } public int top() { return ((Integer)imp.peek()).intValue(); } public boolean isEmpty() { return imp.empty(); } }
8
//=== implementation side ========================= interface StackImp { // an implementation/body base class Object push( Object o ); Object peek(); // top Object pop(); boolean empty(); } class StackImpJava extends java.util.Stack implements StackImp { } class StackImpMine implements StackImp { private Object[] items = new Object[200]; private int total = -1; public Object push( Object o ) { return items[++total] = o; } public Object peek() { return items[total]; } public Object pop() { return items[total--]; } public boolean empty() { return total == -1; } }
9
//==== client code ================================== class BridgeDemo { public static void main( String[] args ) { Stack[] stacks = { new Stack("java"), new Stack("mine"), new StackStomp("java"), new StackStomp("mine") }; //Stack st = new Stack("java"); Stack st = new Stack("mine"); //Stack st = new StackStomp("mine"); st.push(12); st.push(14); st.push(12); st.push(47); st.push(31); System.out.println(st.pop()); System.out.println(st.pop()); System.out.println(st.pop()); for (int num,i=0; i<25; i++) { num = (int)(Math.random() * 1000) % 80; // push this random num onto each stack for (int j=0; j<stacks.length; j++) stacks[j].push(num); } for (int num,i=0; i<stacks.length; i++) { while ( ! stacks[i].isEmpty()) System.out.print( stacks[i].pop() + " " ); System.out.println(); }
10
// add more stack interface variants class StackStomp extends Stack { private int totalStomped = 0; private int max; private int size=0; public StackStomp() { super("java"); max=10;} public StackStomp(String s) { super(s); max=10; } public StackStomp(String s, int size) { super(s); max=size;} public StackStomp(int size) { super("java"); max=size; } public int reportStomped() { return totalStomped; } public void push (int elt) { if (size==max) { // stomp one totalStomped++; Stack st = new Stack(); for (int i=0; i<size; i++) { st.push(super.pop()); } st.pop(); for (int i=0; i<size-1; i++) { super.push(st.pop()); } super.push(elt); } else { // ok to do the push size++; super.push(elt); } public int pop() { if (size>0) { size--; return super.pop(); } else return -1; }
11
Beyond this is just templates
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.