Download Bridge pattern

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
David Stotts
Computer Science Department
UNC Chapel Hill
Software Engineering
(Bridge Design Pattern)

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
//=== 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(); }
}
//=== implementation side =========================
interface
Object
Object
Object
boolean
}
StackImp { // an implementation/body base class
push( Object o );
peek(); // top
pop();
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; }
}
//==== 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());
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.println();
}
}
}
System.out.print( stacks[i].pop() + "
" );
// add more stack interface variants
class StackStomp extends Stack {
private int totalStomped = 0;
private int max;
private int size=0;
public
public
public
public
public
StackStomp() { super("java"); max=10;}
StackStomp(String s) { super(s); max=10; }
StackStomp(String s, int size) { super(s); max=size;}
StackStomp(int size) { super("java"); max=size; }
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;
}
}
Beyond this is just templates