Blog / Design Patterns
The Bridge Design Pattern in C# and .Net
  • Dec 20, 2020
  • 79
  • 92

The second design pattern in the Structural Category is the Bridge Design Pattern. Let's take a look at this design pattern to understand how to apply this pattern in C# and .Net programming.

Note: code can be downloaded at my Github.


Advertisement
 


1. What is the Bridge Design Pattern?

In the Bridge Design Pattern, it is essential to "bridge" the Abstraction and the Implementation of a class. The Implementation is an interface that will describe a set of specific behavior that can be used by any Object in our codebase. It can have multiple concrete implementations that adhere to the contract defined in the interface. The Abstraction is the object that will provide an API that will make use of an underlying Implementation. It acts as a layer over the top of the Implementation that can be further refined through inheritance if required.

Bridge is a structural design pattern that lets you split a large class or a set of closely related classes into two separate hierarchies—abstraction and implementation—which can be developed independently of each other.


2. When to use the Bridge Design Pattern?

Say you have a geometric Shape class with a pair of subclasses: Circle and Square. You want to extend this class hierarchy to incorporate colors, so you plan to create Red and Blue shape subclasses. However, since you already have two subclasses, you’ll need to create four class combinations such as BlueCircle and RedSquare.

Adding new shape types and colors to the hierarchy will grow it exponentially. For example, to add a triangle shape you’d need to introduce two subclasses, one for each color. And after that, adding a new color would require creating three subclasses, one for each shape type. The further we go, the worse it becomes.

3. How to implement the Bridge Design Pattern?

The Bridge pattern attempts to solve the above problem by switching from inheritance to the object composition. What this means is that you extract one of the dimensions into a separate class hierarchy, so that the original classes will reference an object of the new hierarchy, instead of having all of its state and behaviors within one class.

Step 1: Define an Interface for the Implementation

public interface Color
{
    string Log();
}

Step 2: Define an Abstract class for the Abstraction

public abstract class Shape
{
    public string Type { get; set; }
 
    public Color Color { get; set; }
 
    public Shape(Color color, string type)
    {
        Color = color;
        Type = type;
    }
 
    public override string ToString()
    {
        return $"I am a {Color.Log()} {Type}";
    }
}

Step 3: Create a concrete Implementation

public class Red : Color
{
    public string Log()
    {
        return "Red";
    }
}
 
public class Blue : Color
{
    public string Log()
    {
        return "Blue";
    }
}

Step 4: Create a refined Abstraction that behaves slightly different

public class Circle : Shape
{
    public Circle(Color color)
        : base (color, "Circle")
    {
 
    }
}
 
public class Square : Shape
{
    public Square(Color color)
        : base(color, "Square")
    {
 
    }
}

So, we can now initiate circle or square objects by calling the Shape constructor and passing the color object as a parameter like below:

class Program
{
    static void Main(string[] args)
    {
        var circle = new Circle(new Red());
        Console.WriteLine(circle.ToString());
 
        var square = new Square(new Blue());
        Console.WriteLine(square.ToString());
    }
}
Done! By implementing the Bridge Design Pattern like above, we can avoid exponentially growing numbers of shape sub classes and also allow the Implementation and Abstraction grow independently without affecting each other.

I hope this post is helpful! Please share your thoughts under the comment section below. See you next time!


There are some benefits of keeping both UI and API parts in the same place for small projects. In this article, I will explain how I did to deploy Angular Web and ASP .Net Core API in the same folder ...

Using cherry-pick to select specific commits for your Pull Request.1. Create a new branch based on the target of the Pull Requestgit branch cherry-branch origin/master2. Switch to a new branchgit chec ...

I got CORS error after publishing my API and Angular app to IIS even though CORS is enabled and the origins of the Angular app is added. Below is how I resolved this issue.Just simple, make sure you s ...

After deployment Angular and API on IIS, it's working fine unless I refresh the page. Once refreshed, the web encountered 404 error. In this article, I will explain how to resolve this.Since Angular i ...

1. The Situation:Error Message: Pulse Secure Application failed to load Java. Please install correct JRE version.Description: This issue happens when I'm using a M1 Mac with a correct version of ...

Accelerated Mobile Pages (AMP) focuses on delivering static content from publishers as quickly as possible and possibly rewards early adopters with a boost in rank. Let's see how to implement it ...

Below is how to decrypt/convert a Hex string value into text using VB.Net:Decrypting Hex string value to string in VB.Net Function HexToString(ByVal hex As String) As String Dim text As New Sy ...

After a month of publishing on Google Play, Jungle Words has made it to the Top Android Games To Try Out In April 2021. Please check it out! GameKeys.netGameKeys is a website which introduces gam ...

Centering HTML elements is an important aspect for anything involving designing with CSS. There are various methods of centering things, however, it also depends on the elements that you are working w ...