- The Bridge Pattern is a Structural Design Pattern, It split a class or set of closely related classes into two separate hierarchies, abstraction and implementation. It’s allow the abstraction and implementation to be developed independently.
- The client code can access only abstraction part
without being concerned about the implementation part.
Example:
Consider you are creating HouseHold Management Application. A house there are so many commercial appliances like AC, Refrigerator, Fan, TV, Gate Opener and so on…. that you can turn on or off. There are different ways to turn the appliance on or off, such as using switch or using automatic remote control mechanism or manual remote control mechanism. It’s good in application but how about code? In this case automatic remote controller using same switch but different commercial home appliance, on the same time manual remote controller using same switch but different commercial home appliance. To overcome this problem bridge pattern come to this place. It work as bridge interface between abstraction ((i.e.) Automatic Remote Controller, Manual Remote Controller) and implementation ((i.e.) TV, Refrigerator, Gate Opener and so on…..)
UML Diagram:
Drawbacks:
· The bridge interface of the behaviour can require changes over time, which can cause maintenance problems.
· The abstraction and implementation cannot be independently extended or composed.
When we use Adapter Pattern and Bridge Pattern?
· Adapter makes things work after they’re designed; Bridge makes them work before they are
Sample Demo Program:The following participants are involved in given program1. Abstraction – Core of the bridge design pattern. It contains members that define an abstract business object and its functionality. It also contains a reference to an object of type bridge2. Redefined Abstraction – This is a class which inherits from the Abstraction class. It extends the interface defined by Abstraction class.3. Bridge – It’s an Interface which acts as a bridge between the abstraction class and implementation classes and also makes the functionality of implementation class independent from the abstraction class4. Concrete Implementation - It’s a set of classes which implement bridge interface and also provide the implementation details for the associated Abstraction class.
Entire Code Structure:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
/** * Author : Manikandan M * Description : Sample program for bridge pattern */ using System; using System.Threading; namespace BridgePattern { class Program { /// <summary> /// 'Abstraction' class /// </summary> abstract class Switch { public ICommercialHomeAppliance commercialHomeAppliance; public abstract void TurnOn(); public abstract void TurnOff(); } /// <summary> /// 'RefinedAbstraction' class /// </summary> class AutomaticRemoteController: Switch { public override void TurnOn() { commercialHomeAppliance.Start("240 V"); //default voltage } public override void TurnOff() { commercialHomeAppliance.Stop("240 V"); } } /// <summary> /// 'RefinedAbstraction' class /// </summary> class ManualRemoteController : Switch { public string VoltageController { get; set; } public override void TurnOn() { commercialHomeAppliance.Start(VoltageController); //manual voltage } public override void TurnOff() { commercialHomeAppliance.Stop(VoltageController); } } /// <summary> /// 'Bridge' Interface /// </summary> interface ICommercialHomeAppliance { void Start(string voltage); void Stop(string voltage); } /// <summary> /// 'ConcreteImplementation' class /// </summary> class AC : ICommercialHomeAppliance { private string name; public AC(string name) { this.name = name; } public void Start(string voltage) { Console.WriteLine(string.Format("{0} is started. Time: {1}, Voltage: {2}", this.name, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"), voltage)); } public void Stop(string voltage) { Console.WriteLine(string.Format("{0} is stoped. Time: {1}, Voltage: {2}", this.name, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"), voltage)); } } /// <summary> /// 'ConcreteImplementation' class /// </summary> class Refrigerator : ICommercialHomeAppliance { private string name; public Refrigerator(string name) { this.name = name; } public void Start(string voltage) { Console.WriteLine(string.Format("{0} is started. Time: {1}, Voltage: {2}", this.name, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"), voltage)); } public void Stop(string voltage) { Console.WriteLine(string.Format("{0} is stoped. Time: {1}, Voltage: {2}", this.name, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"), voltage)); } } /// <summary> /// 'ConcreteImplementation' class /// </summary> class Fan : ICommercialHomeAppliance { private string name; public Fan(string name) { this.name = name; } public void Start(string voltage) { Console.WriteLine(string.Format("{0} is started. Time: {1}, Voltage: {2}", this.name, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"), voltage)); } public void Stop(string voltage) { Console.WriteLine(string.Format("{0} is stoped. Time: {1}, Voltage: {2}", this.name, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"), voltage)); } } /// <summary> /// 'ConcreteImplementation' class /// </summary> class GateOpener : ICommercialHomeAppliance { private string name; public GateOpener(string name) { this.name = name; } public void Start(string voltage) { Console.WriteLine(string.Format("{0} is started. Time: {1}, Voltage: {2}", this.name, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"), voltage)); } public void Stop(string voltage) { Console.WriteLine(string.Format("{0} is stoped. Time: {1}, Voltage: {2}", this.name, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"), voltage)); } } /// <summary> /// 'ConcreteImplementation' class /// </summary> class Tv : ICommercialHomeAppliance { private string name; public Tv(string name) { this.name = name; } public void Start(string voltage) { Console.WriteLine(string.Format("{0} is started. Time: {1}, Voltage: {2}", this.name, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"), voltage)); } public void Stop(string voltage) { Console.WriteLine(string.Format("{0} is stoped. Time: {1}, Voltage: {2}", this.name, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"), voltage)); } } static void Main(string[] args) { int delayMilliseconds = 2000; ICommercialHomeAppliance ac = new AC("MicroMax Star Split AC"); ICommercialHomeAppliance refrigerator = new Refrigerator("LG Single Door Refrigerator"); ICommercialHomeAppliance fan = new Fan("Usha Fan"); ICommercialHomeAppliance tv = new Tv("Panasonic TV"); ICommercialHomeAppliance gateopener = new GateOpener("GPS 3G Gate Door Opener"); //automatic remote controller Switch automaticRemoteSwitch = new AutomaticRemoteController(); Console.WriteLine("---------------------------Automated Remote Controller---------------------------"); //control gate opener automaticRemoteSwitch.commercialHomeAppliance = gateopener; automaticRemoteSwitch.TurnOn(); Thread.Sleep(delayMilliseconds); automaticRemoteSwitch.TurnOff(); Console.WriteLine("---------------------------------------------------------------------------------"); //manual remote controller ManualRemoteController manualRemoteSwitch = new ManualRemoteController(); Console.WriteLine("---------------------------Manual Remote Controller------------------------------"); manualRemoteSwitch.VoltageController = "220 V"; //control ac manualRemoteSwitch.commercialHomeAppliance = ac; manualRemoteSwitch.TurnOn(); Thread.Sleep(delayMilliseconds); manualRemoteSwitch.TurnOff(); //control refrigerator manualRemoteSwitch.commercialHomeAppliance = refrigerator; manualRemoteSwitch.TurnOn(); Thread.Sleep(delayMilliseconds); manualRemoteSwitch.TurnOff(); //control fan manualRemoteSwitch.commercialHomeAppliance = fan; manualRemoteSwitch.TurnOn(); Thread.Sleep(delayMilliseconds); manualRemoteSwitch.TurnOff(); //control tv manualRemoteSwitch.commercialHomeAppliance = tv; manualRemoteSwitch.TurnOn(); Thread.Sleep(delayMilliseconds); manualRemoteSwitch.TurnOff(); Console.WriteLine("---------------------------------------------------------------------------------"); Console.Write("Press any key to exist..."); Console.ReadKey(); } } }
Click here to download sample program
If you like this post. Kindly share your valuable comments
Software Development Tricky
Wednesday, 4 October 2017
Bridge Pattern
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment