mirror of
https://github.com/farcasclaudiu/design_pattens.git
synced 2026-06-28 21:01:25 +03:00
bridge design pattern
This commit is contained in:
+3
-1
@@ -8,6 +8,7 @@ using design_patterns.creational.facadebuilder;
|
|||||||
using design_patterns.creational.singleton;
|
using design_patterns.creational.singleton;
|
||||||
using design_patterns.creational.prototype;
|
using design_patterns.creational.prototype;
|
||||||
using design_patterns.structural.adapter;
|
using design_patterns.structural.adapter;
|
||||||
|
using design_patterns.structural.bridge;
|
||||||
|
|
||||||
namespace design_patterns
|
namespace design_patterns
|
||||||
{
|
{
|
||||||
@@ -27,7 +28,8 @@ namespace design_patterns
|
|||||||
// await PrototypeSample.Run();
|
// await PrototypeSample.Run();
|
||||||
|
|
||||||
// structural
|
// structural
|
||||||
await AdapterSample.Run();
|
// await AdapterSample.Run();
|
||||||
|
await BridgeSample.Run();
|
||||||
}
|
}
|
||||||
catch (System.Exception ex)
|
catch (System.Exception ex)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -0,0 +1,77 @@
|
|||||||
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace design_patterns.structural.bridge
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 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.
|
||||||
|
/// </summary>
|
||||||
|
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}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user