So I decided to dig into understanding how Asp.net works under the hood, out of curiosity. In the articles that I found the authors described the architecture in lower level details. One of the things that caught my eyes was the use of named Pipes as a means of inter-process communications by the architecture. So I decided to dig into that portion a little bit deeper. I discovered that the .Net framework has a namespace for named pipes and it's quite easy to use. I intend to demonstrate a small example that I cooked up rather quickly by setting up a named pipe server and then connecting a pipe client to it. Before I do that, I want to say as a disclaimer that I don't know that this example demonstrates the best way to use pipes. What it does do is show the most straightforward way to use them. Also, before you use pipes you should consider using something else like WCF instead if you need well defined interfaces, as using pipes is pretty raw. Being the type of perso
Comments
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.
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.
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
}
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. */
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.