Bridge Pattern

Definition:

"Decouple abstraction from its implementation."

Separate abstraction from its implementation. So, both abstraction and implementation can vary independently.

Description:

Abstraction and its implementation is tightly coupled. means, abstract class or interface members needs to be implemented in derived classes. The Bridge pattern provides alternate to this tightly coupled approach.
The Bridge Pattern separates abstract class definition from its implementation classes. This separation helps both abstract class and implement classes to modify separately.

What are you achieving with the help of Bridge Pattern?

The Bridge Pattern helps to maintain different version of abstract class implementations. Its safeguarding the existing abstract class implementation from changes and supporting the new implementations for abstract class.

What is the impact if you don’t use Bridge Pattern?

Abstraction and its implementations are tightly coupled. The changes in abstract classes enforces to make cascading changes to all its implementation classes.

Aim:

Construct user interface view for displaying daily and weekly generated meal plan. This construction process should be capable to support future changes like adding new view types and extending view for displaying monthly generated meal plan.

Use cases:

  1. Construct grid control for displaying daily and weekly meal plan view without bridge pattern
  2. Construct grid and list control for displaying daily and weekly meal plan view without bridge pattern
  3. Construct grid and list control for displaying daily and weekly meal plan view with bridge pattern
  4. Construct grid and list control for displaying daily, weekly & monthly meal plan view with bridge pattern. Add monthly view using bridge pattern without breaking existing implementation

Problem Statement:

The MealPlanView & GridView classes are expected to modify for consecutive enhancements after releasing first version.

Solution:

The MealPlanView & GridView classes should not be modified extensively for consecutive enhancements after releasing first version. The bridge interface shall be introduced to help fixing this issue.

Example Program for Usecase 1:

Usecase 1: Construct grid control for displaying daily and weekly meal plan view without bridge pattern
Description : GridView abstract class with DailyGridView & WeeklyGridView concrete implementations for constructing grid control with Daily & Weekly views.

            class BridgePatternClient
            {
                static void Main(string[] args)
                {
                    MealPlanView mealPlanView = new MealPlanView();
                    mealPlanView.ConstuctViewControl();

                    Console.ReadLine();
                }
            }

            public class MealPlanView
            {
                public void ConstuctViewControl()
                {
                    GridView dailyGridView = new DailyGridView();
                    dailyGridView.Construct();

                    GridView weeklyGridView = new WeeklyGridView();
                    weeklyGridView.Construct();
                }
            }

            public abstract class GridView
            {
                public abstract void Construct();
            }

            public class DailyGridView : GridView
            {
                public override void Construct()
                {
                    Console.WriteLine("Constructed Grid for Daily View");
                }
            }

            public class WeeklyGridView : GridView
            {
                public override void Construct()
                {
                    Console.WriteLine("Constructed Grid for Weekly View");
                }
            }
        

Example Program for Usecase 2:

Usecase 2: Construct grid and list control for displaying daily and weekly meal plan view without bridge pattern
Description : GridView & ListView abstract classes with DailyGridView, WeeklyGridView, DailyListView & WeeklyListView concrete implementations for constructing grid & list controls with Daily & Weekly views.

            class BridgePatternClient
            {
                static void Main(string[] args)
                {
                    Console.WriteLine("Select the view mode you like, a.GridView b.ListView");

                    string viewType = Console.ReadLine();

                    MealPlanView mealPlanView = new MealPlanView();
                    mealPlanView.ConstuctViewControl(viewType);

                    Console.ReadLine();
                }
            }

            public class MealPlanView
            {
                public void ConstuctViewControl(string viewType)
                {
                    switch(viewType)
                    {
                        case "GridView":
                            GridView dailyGirdView = new DailyGridView();
                            dailyGirdView.Construct();

                            GridView weeklyGridView = new WeeklyGridView();
                            weeklyGridView.Construct();
                            break;

                        case "ListView":
                            ListView dailyListView = new DailyListView();
                            dailyListView.Construct();

                            ListView weeklyListView = new WeeklyListView();
                            weeklyListView.Construct();
                            break;
                    }
            
                }
            }

            public abstract class GridView
            {
                public abstract void Construct();
            }

            public class DailyGridView : GridView
            {
                public override void Construct()
                {
                    Console.WriteLine("Constructed Grid for Daily View");
                }
            }

            public class WeeklyGridView : GridView
            {
                public override void Construct()
                {
                    Console.WriteLine("Constructed Grid for Weekly View");
                }
            }

            public abstract class ListView
            {
                public abstract void Construct();
            }

            public class DailyListView : ListView
            {
                public override void Construct()
                {
                    Console.WriteLine("Constructed List for Daily View");
                }
            }

            public class WeeklyListView : ListView
            {
                public override void Construct()
                {
                    Console.WriteLine("Constructed List for Weekly View");
                }
            }

Example Program for Usecase 3:

Usecase 3: Construct grid and list control for displaying daily and weekly meal plan view with bridge pattern
Description :
ControlTypeAbstraction : abstract class with ViewTypeImplementor interface property reference
GridRedefinedAbstraction & ListRedefinedAbstraction : concrete implementation for ControlTypeAbstraction abstract class, which invokes Construct method in ViewTypeImplementor interface using ViewType property
ViewTypeImplementor : contain abstract method for constructing view like DailyView & WeeklyView
DailyView & WeeklyView : concrete class implements ViewTypeImplementor abstract class

            class BridgePatternClient
            {
                static void Main(string[] args)
                {
                    Console.WriteLine("Select the view type you like, a.GridView b.ListView");

                    string viewType = Console.ReadLine();

                    MealPlanView mealPlanView = new MealPlanView();
                    mealPlanView.ConstuctViewControl(viewType);

                    Console.ReadLine();
                }
            }

            public class MealPlanView
            {
                public void ConstuctViewControl(string viewType)
                {
                    ViewTypeImplementor dailyViewGridControl = new DailyView();
                    ViewTypeImplementor weeklyViewGridControl = new WeeklyView();

                    switch (viewType)
                    {
                        case "GridView":                   

                            ControlTypeAbstraction gridControl = new GridRedefinedAbstraction();
                            gridControl.ViewType = dailyViewGridControl;
                            gridControl.ConstructControl();

                            gridControl.ViewType = weeklyViewGridControl;
                            gridControl.ConstructControl();

                            break;

                        case "ListView":

                            ControlTypeAbstraction listControl = new ListRedefinedAbstraction();
                            listControl.ViewType = dailyViewGridControl;
                            listControl.ConstructControl();

                            listControl.ViewType = weeklyViewGridControl;
                            listControl.ConstructControl();

                            break;
                    }

                }
            }

            public abstract class ControlTypeAbstraction
            {
                public ViewTypeImplementor ViewType { get; set; }
                public abstract void ConstructControl();
            }

            public class GridRedefinedAbstraction : ControlTypeAbstraction
            {
                public override void ConstructControl()
                {
                    ViewType.Construct();
                }
            }

            public class ListRedefinedAbstraction : ControlTypeAbstraction
            {
                public override void ConstructControl()
                {
                    ViewType.Construct();
                }
            }

            public abstract class ViewTypeImplementor
            {
                public abstract void Construct();
            }

            public class DailyView : ViewTypeImplementor
            {
                public override void Construct()
                {
                    Console.WriteLine("Constructed Daily View");
                }
            }

            public class WeeklyView : ViewTypeImplementor
            {
                public override void Construct()
                {
                    Console.WriteLine("Constructed Weekly View");
                }
            }

Example Program for Usecase 4:

Usecase 4: Construct grid and list control for displaying daily, weekly & monthly meal plan view with bridge pattern. Add monthly view using bridge pattern without breaking existing implementation
Description :
ControlTypeAbstraction : abstract class with ViewTypeImplementor interface property reference
GridRedefinedAbstraction & ListRedefinedAbstraction : concrete implementation for ControlTypeAbstraction abstract class, which invokes Construct method in ViewTypeImplementor interface using ViewType property
ViewTypeImplementor : contain abstract method for constructing view like DailyView & WeeklyView
DailyView , WeeklyView & MonthlyView : concrete class implements ViewTypeImplementor abstract class

            class BridgePatternClient
            {
                static void Main(string[] args)
                {
                    Console.WriteLine("Select the view type you like, a.GridView b.ListView");

                    string viewType = Console.ReadLine();

                    MealPlanView mealPlanView = new MealPlanView();
                    mealPlanView.ConstuctViewControl(viewType);

                    Console.ReadLine();
                }
            }

            public class MealPlanView
            {
                public void ConstuctViewControl(string viewType)
                {
                    ViewTypeImplementor dailyViewGridControl = new DailyView();
                    ViewTypeImplementor weeklyViewGridControl = new WeeklyView();
                    //Added support MonthlyView
                    ViewTypeImplementor monthlyViewGridControl = new MonthlyView();

                    switch (viewType)
                    {
                        case "GridView":
                            ControlTypeAbstraction gridControl = new GridRedefinedAbstraction();
                            gridControl.ViewType = dailyViewGridControl;
                            gridControl.ConstructControl();

                            gridControl.ViewType = weeklyViewGridControl;
                            gridControl.ConstructControl();

                            gridControl.ViewType = monthlyViewGridControl;
                            gridControl.ConstructControl();

                            break;

                        case "ListView":
                            ControlTypeAbstraction listControl = new ListRedefinedAbstraction();
                            listControl.ViewType = dailyViewGridControl;
                            listControl.ConstructControl();

                            listControl.ViewType = weeklyViewGridControl;
                            listControl.ConstructControl();

                            listControl.ViewType = monthlyViewGridControl;
                            listControl.ConstructControl();

                            break;
                    }

                }
            }

            public abstract class ControlTypeAbstraction
            {
                public ViewTypeImplementor ViewType { get; set; }
                public abstract void ConstructControl();
            }

            public class GridRedefinedAbstraction : ControlTypeAbstraction
            {
                public override void ConstructControl()
                {
                    ViewType.Construct();
                }
            }

            public class ListRedefinedAbstraction : ControlTypeAbstraction
            {
                public override void ConstructControl()
                {
                    ViewType.Construct();
                }
            }

            public abstract class ViewTypeImplementor
            {
                public abstract void Construct();
            }

            public class DailyView : ViewTypeImplementor
            {
                public override void Construct()
                {
                    Console.WriteLine("Constructed Daily View");
                }
            }

            public class WeeklyView : ViewTypeImplementor
            {
                public override void Construct()
                {
                    Console.WriteLine("Constructed Weekly View");
                }
            }

            public class MonthlyView : ViewTypeImplementor
            {
                public override void Construct()
                {
                    Console.WriteLine("Constructed Monthly View");
                }
            }