From 6244d508cd20be0e2adab05cfb684475cd5f074b Mon Sep 17 00:00:00 2001 From: Claudiu Farcas Date: Tue, 20 Apr 2021 22:28:01 +0300 Subject: [PATCH] bridge design pattern --- Program.cs | 4 +- structural/bridge/BridgeSample.cs | 77 +++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 structural/bridge/BridgeSample.cs diff --git a/Program.cs b/Program.cs index 1f4f950..16ec312 100644 --- a/Program.cs +++ b/Program.cs @@ -8,6 +8,7 @@ using design_patterns.creational.facadebuilder; using design_patterns.creational.singleton; using design_patterns.creational.prototype; using design_patterns.structural.adapter; +using design_patterns.structural.bridge; namespace design_patterns { @@ -27,7 +28,8 @@ namespace design_patterns // await PrototypeSample.Run(); // structural - await AdapterSample.Run(); + // await AdapterSample.Run(); + await BridgeSample.Run(); } catch (System.Exception ex) { diff --git a/structural/bridge/BridgeSample.cs b/structural/bridge/BridgeSample.cs new file mode 100644 index 0000000..a475bae --- /dev/null +++ b/structural/bridge/BridgeSample.cs @@ -0,0 +1,77 @@ +using System; +using System.Threading.Tasks; + +namespace design_patterns.structural.bridge +{ + /// + /// 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. + /// + /// - Used when you want to divide and organize a monolithic class. + /// - Used when you need to extend a class in several orthogonal + /// (independent) dimensions. + /// - Used if you need to be able to switch implementations at runtime. + /// + public class BridgeSample + { + public static async Task Run() + { + Console.WriteLine("Structural - Bridge"); + + var notification = new Notification { + Category = "Cat1", + Priority = "High", + Message = "Something happend" + }; + var notSender = new NotificationSender(new EmailSenderService()); + notSender.SendNotification(notification); + notSender = new NotificationSender(new SmsSenderService()); + notSender.SendNotification(notification); + } + } + + public class Notification + { + public string Category { get; set; } + public string Message { get; set; } + public string Priority { get; set; } + } + + // Abstraction + public class NotificationSender + { + private readonly ISenderService senderservice; + public NotificationSender(ISenderService senderservice) + { + this.senderservice = senderservice; + } + + public void SendNotification (Notification notification) { + var message = $"\tCategory: {notification.Category}\n\r\tPriority: {notification.Priority}\n\r\tMessage: {notification.Message}"; + senderservice.SendMessage(message); + } + } + + // Implementation + public interface ISenderService { + void SendMessage(string message); + } + + public class EmailSenderService : ISenderService + { + public void SendMessage(string message) + { + System.Console.WriteLine($"Emailing message:\n\r{message}"); + } + } + + public class SmsSenderService : ISenderService + { + public void SendMessage(string message) + { + System.Console.WriteLine($"Sms message:\r\n{message}"); + } + } +} \ No newline at end of file