0% found this document useful (0 votes)
93 views

Bridge Pattern

The document describes the Bridge pattern, which decouples an abstraction from its implementation so that the two can vary independently. It provides an example of a Meal abstraction with different cuisine implementations like American, Italian, and French. The abstraction and implementations are all extensible via subclassing. The pattern isolates implementation changes from clients and allows sharing implementations between objects.

Uploaded by

hrishiking
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
93 views

Bridge Pattern

The document describes the Bridge pattern, which decouples an abstraction from its implementation so that the two can vary independently. It provides an example of a Meal abstraction with different cuisine implementations like American, Italian, and French. The abstraction and implementations are all extensible via subclassing. The pattern isolates implementation changes from clients and allows sharing implementations between objects.

Uploaded by

hrishiking
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 12

by

Robert Smith
Feb 23, 2009
 The normal method of dealing with an abstraction
having several implementations is through
inheritance.
 However, this permanently binds the
implementation to the abstraction.
 This is not always flexible enough. You may want
to modify or extend the abstraction or
implementations independently.

 The Bridge Pattern decouples an abstraction from


its implementation.
 Selection or switching of implementation is at run-
time rather than design time.
 Abstraction and implementation should both be
extensible by subclassing.
 You want to isolate implementation changes from
clients.
 You want to share an implementation among
multiple objects and this should be hidden from
the client.
bridge
bridge
public class Meal { public class Snack extends Meal {
protected MealImp imp; public void getSnack() {
imp.getAppetizer();
public Meal() { }
imp = new AmericanMealImp(); }
}

public Meal(String type) {


if (type.equals("American")) public class Lunch extends Meal {
imp = new AmericanMealImp(); public void getLunch() {
if (type.equals("Italian")) imp.getSandwich();
imp = new ItalianMealImp(); imp.getBeverage();
if (type.equals("French")) }
imp = new FrenchMealImp(); }
}

public void getFirstCourse() { public class FiveCourseMeal extends Meal {


imp.getAppetizer(); public void getEnormousDinner() {
} imp.getAppetizer();
imp.getSorbet();
public void getSecondCourse() { imp.getSoup();
imp.getMeat(); imp.getSorbet();
} imp.getSalad();
imp.getSorbet();
public void getBeverage() { imp.getFish();
imp.getBeverage(); imp.getSorbet();
} imp.getMeat();
imp.getSorbet();
public void getDessert() { imp.getCheesePlate();
imp.getDessert(); imp.getDessert();
} imp.getBeverage();
} }
}
public interface MealImp { public class ItalianMealImp implements MealImp {
public abstract void getAppetizer(); public void getAppetizer() {
public abstract void getSoup(); System.out.println("Appetizer: anti pasti"); }
public abstract void getSalad(); public void getSoup() {}
public abstract void getFish(); public void getSalad() {}
public abstract void getMeat(); public void getFish() {}
public abstract void getPasta(); public void getMeat() {
public abstract void getCheesePlate(); System.out.println("Meat: chicken piccata"); }
public abstract void getBeverage(); public void getPasta() {}
public abstract void getDessert(); public void getCheesePlate() {}
public abstract void getSorbet(); public void getBeverage() {
public abstract void getSandwich(); System.out.println("Beverage: cappuccino"); }
} public void getDessert() {
System.out.println("Dessert: gelato"); }
public void getSorbet() {}
public void getSandwich() {}
}
public class AmericanMealImp implements MealImp {
public void getAppetizer() {
System.out.println("Appetizer: nachos"); }
public void getSoup() {}
public class FrenchMealImp implements MealImp {
public void getSalad() {}
public void getAppetizer() {
public void getFish() {}
System.out.println("Appetizer: escargot"); }
public void getMeat() {
public void getSoup() {}
System.out.println("Meat: steak"); }
public void getSalad() {}
public void getPasta() {}
public void getFish() {}
public void getCheesePlate() {}
public void getMeat() {
public void getBeverage() {
System.out.println("Meat: coq au vin"); }
System.out.println("Beverage: beer"); }
public void getPasta() {}
public void getDessert() {
public void getCheesePlate() {}
System.out.println("Dessert: apple pie"); }
public void getBeverage() {
public void getSorbet() {}
System.out.println("Beverage: bordeaux"); }
public void getSandwich() {}
public void getDessert() {
}
System.out.println("Dessert: creme brulee"); }
public void getSorbet() {}
public void getSandwich() {}
}
public class Customer {

private Meal meal;

public Customer(Meal aMeal) { meal = aMeal; }

public void eat() {


meal.getFirstCourse();
meal.getSecondCourse();
meal.getBeverage();
meal.getDessert();
}

public static void main(String[] args) {


Meal aMeal = null;

// Read the input to see if we do American, Italian or French


if (args.length == 0) {
// create default Meal
aMeal = new Meal();
}
else if (args.length == 1) {
// see if arg is NOT American, Italian or French
if (!(args[0].equals("American")) && !(args[0].equals("Italian")) && !(args[0].equals("French"))) {
System.err.println("1 arg given but not recognized");
System.err.println("usage: java Customer [American|Italian|French]");
System.exit(1);
}
else {
aMeal = new Meal(args[0]);
}
}
else { // error
System.err.println("wrong number of args");
System.err.println("usage: java Customer [American|Italian|French]");
System.exit(1);
}

Customer c = new Customer(aMeal);


c.eat();
}
}
Bob@Bob-XPS ~/cspp51023/Bridge Pattern
$ java Customer American
Appetizer: nachos
Meat: steak
Beverage: beer
Dessert: apple pie

Bob@Bob-XPS ~/cspp51023/Bridge Pattern


$ java Customer Italian
Appetizer: anti pasti
Meat: chicken piccata
Beverage: cappuccino
Dessert: gelato

Bob@Bob-XPS ~/cspp51023/Bridge Pattern


$ java Customer French
Appetizer: escargot
Meat: coq au vin
Beverage: bordeaux
Dessert: creme brulee

Bob@Bob-XPS ~/cspp51023/Bridge Pattern


$
 Decouples interface and implementation.
◦ Eliminates compile-time dependencies.
 Improved extensibility.
 Hiding implementation details from client.
 Only one Implementor
◦ Multiple implementors not always needed.
◦ Separation is still useful even in this degenerate case to
hide implementation changes from clients.
 Creating correct Implementor
◦ Instantiate in constructor based on parameters.
◦ Use default implementation and possibly change later.
◦ Use AbstractFactory.
 Sharing Implementors
◦ Use a reference count in the implementation to know when
an instance is being used.
Abstract Factory -
Used to create the Implementor and remove all
knowledge of which one from the Abstraction.

Adapter –
Used to make 2 unrelated classes work together.
Usually applied after system is designed. Bridge
does similar work but is done up front during
design.

You might also like