Property of Penn Engineering
Bridge Pattern
Copyright By PowCoder代写 加微信 powcoder
Property of Penn Engineering
• Creational: how objects are created
• Structural: how objects are composed
• Behavioral: how objects communicate
Categories of Design Patterns
Property of Penn Engineering
• Creational: how objects are created
• Structural: how objects are composed
• Behavioral: how objects communicate
Categories of Design Patterns
Property of Penn Engineering
• Address problems such as:
• how do we assemble the constituent parts of an object?
• how do we define the relationships between classes?
• how do we combine objects into larger structures?
• Benefits: changeability, stability
Structural Design Patterns
Property of Penn Engineering
• Problem: want to allow for combinations of concepts (abstractions)
and the things that use them (implementors)
• Concern: if we have M abstractions and N implementors, we would
have M*N classes to represent the combinations
• Solution: decouple abstraction and implementor so that both may
vary independently
• Maintain separate inheritance hierarchies
• “Bridge” them using aggregation
• Client only needs to work with abstraction
Design Problem
Property of Penn Engineering
• A graphics package is able to draw different Shapes using
different Renderers (e.g. color, grayscale, 3D, etc.).
• Each Renderer should be able to draw each Shape, but we
want to avoid having separate classes to represent each
combination, e.g. ColorCircle, ColorRectangle,
GrayscaleRectangle, etc.
• We can apply the Bridge Pattern to solve this problem
Property of Penn Engineering
public interface Renderer {
void drawCircle(int x, int y,
int radius);
void drawRectangle(int x1,
int x2, int y1, int y2);
public class ColorRenderer
implements Renderer { . . . }
public class GreyscaleRenderer
implements Renderer { . . . }
Property of Penn Engineering
Property of Penn Engineering
public abstract class Shape {
protected Renderer renderer;
public Shape(Renderer r) {
renderer = r;
public abstract void draw( );
Shape Class
Property of Penn Engineering
public class Circle extends Shape {
protected int x, y, radius;
public Circle(int x, int y, int radius,
Renderer renderer) {
super(renderer);
public void draw( ) {
renderer.drawCircle(x, y, radius);
Circle Class
Property of Penn Engineering
public class Circle extends Shape {
protected int x, y, radius;
public Circle(int x, int y, int radius,
Renderer renderer) {
super(renderer);
public void draw( ) {
renderer.drawCircle(x, y, radius);
Circle Class
Property of Penn Engineering
Shape grayCircle = new Circle(50, 50, 20, new GrayscaleRenderer());
Shape colorRectangle = new Rectangle(80, 80, 120, 120, new ColorRenderer());
if (condition) {
performAction(grayCircle);
performAction(colorRectangle);
Using the Shapes
void performAction(Shape s) {
Property of Penn Engineering
• Structural design patterns: Decouple abstraction and
implementation so that both may vary independently
• Bridge Pattern: Maintain separate inheritance hierarchies and
“bridge” them using aggregation so that Client only needs to
work with abstraction
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com