By Robert Smith Feb 23, 2009.  The normal method of dealing with an abstraction having several implementations is through inheritance.  However, this.

Slides:



Advertisements
Similar presentations
Design Patterns.
Advertisements

Creational Patterns, Abstract Factory, Builder Billy Bennett June 11, 2009.
Chapter 10: Introduction to Inheritance
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Bridge Pattern.
The Bridge Pattern.. Intent Decouple an abstraction from its implementation so that the two can vary independently Also known as: Handle/Body.
Design Patterns A General reusable solution to a commonly occurring problem in software design. Creational: Deal with object creation mechanisms – Example:
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 1 Object-Oriented.
The Bridge Pattern Guang Hu February Overview MotivationMotivation ParticipantsParticipants  Structure  Applicability  Benefits  Drawbacks 
Bridge The decoupling of abstraction and implementation.
Patterns Lecture 2. Singleton Ensure a class only has one instance, and provide a global point of access to it.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
1 Introduction to CS Agenda Syllabus Schedule Lecture: the management of complexity.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Chapter 8 Object Design Reuse and Patterns. Finding Objects The hardest problems in object-oriented system development are: –Identifying objects –Decomposing.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Lecture 9 Concepts of Programming Languages
Design Pattern – Bridge (Structural) References Yih-shoung Chen, Department of Information Engineering, Feng Chia University,Taiwan, R.O.C. The Bridge.
Programming Languages and Paradigms Object-Oriented Programming.
Question of the Day  Write valid mathematical equation using: one addition operator (‘+’) one equality operator (‘=’)  Should have equal values.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Applications Development Objects in Java Key concepts and metaphors Implicit subclassing Static methods Constructors Instance.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Software Components Creational Patterns.
Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.
The Factory Patterns SE-2811 Dr. Mark L. Hornick 1.
Chapter 5: Ball Worlds Features 2 classes, constant data fields, constructors, extension through inheritance, graphics.
Programming in Java CSCI-2220 Object Oriented Programming.
Factory Method Explained. Intent  Define an interface for creating an object, but let subclasses decide which class to instantiate.  Factory Method.
Creational Pattern: Factory Method At times, a framework is needed to standardize the behavior of objects that are used in a range of applications, while.
Static Attributes and Inheritance  static attributes behave the same as non-static attributes in inheritance  public and protected static attributes.
Review Class Inheritance, Abstract, Interfaces, Polymorphism, GUI (MVC)
Inheritance and Access Control CS 162 (Summer 2009)
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
1 Abstract Classes and Interfaces. 2 The abstract Modifier  The abstract class –Cannot be instantiated –Should be extended and implemented in subclasses.
Object Oriented Programming
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Pattern Bridge. Definition Bridge is the structural pattern that separates abstraction from the implementation so that both of them can be changed independently.
Interfaces and Polymorphism CS 162 (Summer 2009).
Advanced Object-oriented Design Patterns Creational Design Patterns.
Bridge Bridge is used when we need to decouple an abstraction from its implementation so that the two can vary independently. This type of design pattern.
Programming in java Packages Access Protection Importing packages Java program structure Interfaces Why interface Defining interface Accessing impln thru.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
The Decorator Pattern (Structural) ©SoftMoore ConsultingSlide 1.
Singleton Pattern. Problem Want to ensure a single instance of a class, shared by all uses throughout a program Context Need to address initialization.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
CS 350 – Software Design The Decorator Pattern – Chapter 17 In this chapter we expand our e-commerce case study and learn how to use the Decorator Pattern.
Design Patterns Creational Patterns. Abstract the instantiation process Help make the system independent of how its objects are created, composed and.
9.1 CLASS (STATIC) VARIABLES AND METHODS Defining classes is only one aspect of object-oriented programming. The real power of object-oriented programming.
MPCS – Advanced java Programming
Factory Patterns 1.
Inheritance and Polymorphism
Interface.
Chapter 11 Object-Oriented Design
Creational Pattern: Prototype
Section 11.1 Class Variables and Methods
Lecture 9 Concepts of Programming Languages
Interface.
Interfaces.
Advanced Java Programming
METHOD OVERRIDING in JAVA
BRIDGE PATTERN.
Java Inheritance.
Structural Patterns: Adapter and Bridge
Fundaments of Game Design
Chapter 14 Abstract Classes and Interfaces
Software Design Lecture : 28.
CSG2H3 Object Oriented Programming
Lecture 9 Concepts of Programming Languages
Presentation transcript:

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

public class Meal { protected MealImp imp; public Meal() { imp = new AmericanMealImp(); } public Meal(String type) { if (type.equals("American")) imp = new AmericanMealImp(); if (type.equals("Italian")) imp = new ItalianMealImp(); if (type.equals("French")) imp = new FrenchMealImp(); } public void getFirstCourse() { imp.getAppetizer(); } public void getSecondCourse() { imp.getMeat(); } public void getBeverage() { imp.getBeverage(); } public void getDessert() { imp.getDessert(); } public class Snack extends Meal { public void getSnack() { imp.getAppetizer(); } public class Lunch extends Meal { public void getLunch() { imp.getSandwich(); imp.getBeverage(); } public class FiveCourseMeal extends Meal { public void getEnormousDinner() { imp.getAppetizer(); imp.getSorbet(); imp.getSoup(); imp.getSorbet(); imp.getSalad(); imp.getSorbet(); imp.getFish(); imp.getSorbet(); imp.getMeat(); imp.getSorbet(); imp.getCheesePlate(); imp.getDessert(); imp.getBeverage(); }

public interface MealImp { public abstract void getAppetizer(); public abstract void getSoup(); public abstract void getSalad(); public abstract void getFish(); public abstract void getMeat(); public abstract void getPasta(); public abstract void getCheesePlate(); public abstract void getBeverage(); public abstract void getDessert(); public abstract void getSorbet(); public abstract void getSandwich(); } public class AmericanMealImp implements MealImp { public void getAppetizer() { System.out.println("Appetizer: nachos"); } public void getSoup() {} public void getSalad() {} public void getFish() {} public void getMeat() { System.out.println("Meat: steak"); } public void getPasta() {} public void getCheesePlate() {} public void getBeverage() { System.out.println("Beverage: beer"); } public void getDessert() { System.out.println("Dessert: apple pie"); } public void getSorbet() {} public void getSandwich() {} } public class ItalianMealImp implements MealImp { public void getAppetizer() { System.out.println("Appetizer: anti pasti"); } public void getSoup() {} public void getSalad() {} public void getFish() {} public void getMeat() { System.out.println("Meat: chicken piccata"); } public void getPasta() {} public void getCheesePlate() {} public void getBeverage() { System.out.println("Beverage: cappuccino"); } public void getDessert() { System.out.println("Dessert: gelato"); } public void getSorbet() {} public void getSandwich() {} } public class FrenchMealImp implements MealImp { public void getAppetizer() { System.out.println("Appetizer: escargot"); } public void getSoup() {} public void getSalad() {} public void getFish() {} public void getMeat() { System.out.println("Meat: coq au vin"); } public void getPasta() {} public void getCheesePlate() {} public void getBeverage() { System.out.println("Beverage: bordeaux"); } 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(); }

~/cspp51023/Bridge Pattern $ java Customer American Appetizer: nachos Meat: steak Beverage: beer Dessert: apple pie ~/cspp51023/Bridge Pattern $ java Customer Italian Appetizer: anti pasti Meat: chicken piccata Beverage: cappuccino Dessert: gelato ~/cspp51023/Bridge Pattern $ java Customer French Appetizer: escargot Meat: coq au vin Beverage: bordeaux Dessert: creme brulee ~/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.