November 8, 2010 Design Patterns
November 8, 2010 Design Patterns
Imagine that you own huge building company. It builds houses and apartment blocks all around your city. Building are of two types – either built with concrete blocks or with bricks. Since you are boss when you were deciding how to divide work you have decided that all crews will have same operations like BuildFoundation, BuildRoom, BuildRoof. But because houses are of two types you had to always keep two teams (aka. two concrete).
Once it turned out that couple of buildings were of mixed type where you had to build some rooms with concrete blocks and some with bricks. For this reason you had to move whole teams from one part of the city to another. You got many complains from your employees, they suggested moving only that part of team that is specialized in building rooms, so you can easily move small crew of workers and reassign it easily to any team instead of having two separate teams. And that is idea.
BRIDGE
Bridge is design pattern that allows you decouple realization from its abstraction, therefore your realization can be changed separately from abstraction because does not implement it directly.
In other words, our IBuildingCompany might have two concrete realizations like NearSeeBuildingCompany and CityBuildingCompany, each of them does probably something different for their foundation and roof, but at the same time we can easily and quickly reassign WallCreator crew for each of those companies to build either brick or either concrete walls.
Let say we have BuildingCompany as below:
internal interface IBuldingCompany { void BuildFoundation(); void BuildRoom(); void BuildRoof(); IWallCreator WallCreator { get; set; } } internal class BuldingCompany : IBuldingCompany { public void BuildFoundation() { Console.WriteLine("Foundation is built.{0}", Environment.NewLine); } public void BuildRoom() { WallCreator.BuildWallWithDoor(); WallCreator.BuildWall(); WallCreator.BuildWallWithWindow(); WallCreator.BuildWall(); Console.WriteLine("Room finished.{0}", Environment.NewLine); } public IWallCreator WallCreator { get; set; } public void BuildRoof() { Console.WriteLine("Roof is done.{0}", Environment.NewLine); } }
So what is so interested about it? Answer is property WallCreator, which is exactly our bridge to implementation.
Usage code
Let see this in action:
// We have two wall creating crews - concrete blocks one and bricks one var brickWallCreator = new BrickWallCreator(); var conctreteSlabWallCreator = new ConcreteSlabWallCreator(); var buildingCompany = new BuldingCompany(); buildingCompany.BuildFoundation(); buildingCompany.WallCreator = conctreteSlabWallCreator; buildingCompany.BuildRoom(); // Company can easily switch to another wall crew to continue building rooms // with another material buildingCompany.WallCreator = brickWallCreator; buildingCompany.BuildRoom(); buildingCompany.BuildRoom(); buildingCompany.BuildRoof();
Isn’t it awesome? Output should be intuitive, but since I did not show implementations of BrickWallCreator and ConcreteSlabWallCreator I’m going to list it below:
Foundation is built.
Concrete slab wall with door.
Concrete slab wall.Concrete slab wall with window.
Concrete slab wall.
Room finished.
Brick wall with door.
Brick wall.Brick wall with window.
Brick wall.
Room finished.
Brick wall with door.
Brick wall.Brick wall with window.
Brick wall.
Room finished.
Roof is done.
UML diagram illustrates how this pattern looks like and why it is called bridge.
Markdown | Result |
---|---|
*text* | text |
**text** | text |
***text*** | text |
`code` | code |
~~~ more code ~~~~ |
more code |
[Link](https://www.example.com) | Link |
* Listitem |
|
> Quote | Quote |
Very nice explanation, thanks.
Thank you!