Difference Between Adapter and Bridge Design Patterns

This post has migrated to https://beginningprogrammer.com/programming/difference-between-adapter-and-bridge-pattern/

Comments

Nolan Egly said…
I think you're watering the bridge down to a strategy pattern.

Strategy (and template) provide abstraction of an implementation. Bridge not only abstracts an implementation, but allows the abstraction and implementation to vary independently. This is especially useful to control cominatorial explosion.

For example, you might have a graphics API for a screen display, and another graphics API for a printer or plotter. We need to be able to change between GraphicsAPI and IMyDrawingApi without caring whether we're working with a plotter or a screen (avoid GraphicsAPI.DrawOnScreen() and GraphicsAPI.DrawOnPlotter().

Bridge provides this. It involves separating what was combined in a single concern into two different abstractions, each with multiple implementations. Strategy and template simply provide one abstraction over multiple implementations.
Fernando Zamora said…
Isn't that what IMyDrawingApi does? The IMyDrawingApi could be tied to a plotter or a printer. The client does not care. In this case I only demo'ed the implemention of an Adapter against one API. It could have other implementations (adapters). In other words the ClientWithBridgeAdapter would have other implementations. The _myDrawingAdapter would be adapted to either a plotter or a printer.

Take a look at the sample in wikipedia (http://en.wikipedia.org/wiki/Bridge_pattern). They use the same basic concept except that instead of an interface they use an abstract class.
Fernando Zamora said…
The drawingAdapter (which should be _myDrawingAdapter) within the ClientWithBridgeAdapter will allow the abstraction and implementation to vary independently.
Fernando Zamora said…
I think I see the point you are making Nolan. I think that I missed the point with the bridge. The bridge should be more like this


public class DrawingBridge
{
private IMyDrawingApi _myDrawingAdapter;

public DrawingBridge(IMyDrawingApi drawingAdapter)
{
_myDrawingAdapter = drawingAdapter;
}

public void DrawCircle(int centerX, centerY, int radius)
{
_myDrawingAdapter.DrawCircle(centerX, centerY, radius);
}

...Same for Rectangle

}
Nolan Egly said…
// Here's what we start with
public class Display {
void DrawPixel(int x, int y, int rgbColor);
}

/* Note we're already using strategy pattern to abstract drawing on screen */
public interface Drawing
{
void DrawLine(Point topLeft, Point topRight);
}

public class SlowAndQualityDrawing : Drawing {
void DrawLine(Point topLeft, Point bottomRight)
{ /* Use Display.DrawPixel to draw simple line */ }
}

public class FastAndDraftDrawing : Drawing {
void DrawLine(Point topLeft, Point bottomRight)
{ /* Use Display.DrawPixel to draw antialiased line */ }
}

/* Now, we need to support plotters in addition to screen displays. In order to avoid four drawing methods
(SlowAndQualityDisplay, SlowAndQualityPlotter, FastAndDraftDisplay, SlowAndQualityPlotter) we need to decouple the medium from the display quality. Now we have TWO abstractions - quality and the media being rendered on. Enter the bridge between media and drawing... */

public interface Media {
void MarkSpot(int x, int y, int rgbColor);
}

public class Display : Media {
void MarkSpot(int x, int y, int rgbColor) {
/* draw a pixel */ }
}
public class Plotter : Media {
void MarkSpot(int x, int y, int rgbColor) {
/* move plotter to spot, lower head to mark paper */ }
}

public class SlowAndQualityDrawing : Drawing {
void DrawLine(Point topLeft, Point topRight)
{ /* Use Media.MarkSpot to draw simple line */ }
}

public class FastAndDraftDrawing : Drawing
{
void DrawLine(Point topLeft, Point bottomRight)
{ /* Use Media.MarkSpot to draw antialiased line */ }
}

/* There is no standalone "bridge" object, rather the bridge is between the drawing implementations
and the media implementations. We've made a bridge by splitting out responsibility for an aspect
of drawing into a second abstraction. Now the Media and Drawing implementations can vary without
effecting each other. Note we still have only two drawing methods, instead of the four we'd end up
with if we didn't create the media-drawing bridge. */
Fernando Zamora said…
I think your example is valid. I think that the pattern that I described just happens to be a different implementation. Think of the Bridge as you SlowAndQualityDrawing. You could implement other implementations of Bridge if you wanted. I am not sure that I see much of a difference though. The example that I provided pretty much goes in line with everything that I am understanding about the bridge pattern to include the example on Wikipedia(http://en.wikipedia.org/wiki/Bridge_pattern).

Also I think that patterns can differ in implementation. As long as they are serving their purpose, then they have met their goal.
Nolan Egly said…
Also I think that patterns can differ in implementation. As long as they are serving their purpose, then they have met their goal.

I completely agree with you. I expanded the example simply to illustrate a more concrete sense (media and quality) of two differing concerns being bridged by separating them into two different abstractions.

One of the problems with patterns is struggling with slightly differing implementations being classified differently by different people. They're still very much worth studying and discussing though.

Popular posts from this blog

Why I Hate Regular Expressions

Simple Example of Using Pipes with C#

Why a Programmer Must Have Sales Skills