Add code examples, and documentation
This commit is contained in:
13
patterns/bridge/Bridge Example/AlertNotification.cs
Normal file
13
patterns/bridge/Bridge Example/AlertNotification.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
namespace Bridge_Example;
|
||||
|
||||
//This is one of the refined abstraction classes. Note that it can make use of _sender since it inherits it from its superclass. Note also that since EmailSender and SmsSender implement IMessageSender, their specific SendMessage() method will be called, depending on what's passed into the constructor (see the main method)
|
||||
public class AlertNotification : Notification
|
||||
{
|
||||
public AlertNotification(IMessageSender sender)
|
||||
: base(sender) { }
|
||||
|
||||
public override void Notify(string message)
|
||||
{
|
||||
_sender.SendMessage($"ALERT: {message}");
|
||||
}
|
||||
}
|
||||
7
patterns/bridge/Bridge Example/Bridge Example.csproj
Normal file
7
patterns/bridge/Bridge Example/Bridge Example.csproj
Normal file
@@ -0,0 +1,7 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<RootNamespace>Bridge_Example</RootNamespace>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
16
patterns/bridge/Bridge Example/EmailSender.cs
Normal file
16
patterns/bridge/Bridge Example/EmailSender.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
#region
|
||||
|
||||
using System;
|
||||
|
||||
#endregion
|
||||
|
||||
namespace Bridge_Example;
|
||||
|
||||
//This is one of the concrete implementors, which implements the implementor interface IMessageSender
|
||||
public class EmailSender : IMessageSender
|
||||
{
|
||||
public void SendMessage(string message)
|
||||
{
|
||||
Console.WriteLine($"Email sent: {message}");
|
||||
}
|
||||
}
|
||||
7
patterns/bridge/Bridge Example/IMessageSender.cs
Normal file
7
patterns/bridge/Bridge Example/IMessageSender.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace Bridge_Example;
|
||||
|
||||
//This class is the implementor. It gets implemented by concrete classes.
|
||||
public interface IMessageSender
|
||||
{
|
||||
void SendMessage(string message);
|
||||
}
|
||||
14
patterns/bridge/Bridge Example/Notification.cs
Normal file
14
patterns/bridge/Bridge Example/Notification.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
namespace Bridge_Example;
|
||||
|
||||
//This is the abstraction
|
||||
public abstract class Notification
|
||||
{
|
||||
protected readonly IMessageSender _sender;
|
||||
|
||||
protected Notification(IMessageSender sender)
|
||||
{
|
||||
_sender = sender;
|
||||
}
|
||||
|
||||
public abstract void Notify(string message);
|
||||
}
|
||||
17
patterns/bridge/Bridge Example/Program.cs
Normal file
17
patterns/bridge/Bridge Example/Program.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
namespace Bridge_Example;
|
||||
|
||||
internal class Program
|
||||
{
|
||||
private static void Main(string[] args)
|
||||
{
|
||||
Notification n1 = new AlertNotification(new EmailSender());
|
||||
Notification n2 = new AlertNotification(new SmsSender());
|
||||
Notification n3 = new ReminderNotification(new EmailSender());
|
||||
Notification n4 = new ReminderNotification(new SmsSender());
|
||||
|
||||
n1.Notify("Server is down");
|
||||
n2.Notify("Server is down");
|
||||
n3.Notify("Meeting at 3pm");
|
||||
n4.Notify("Meeting at 3pm");
|
||||
}
|
||||
}
|
||||
13
patterns/bridge/Bridge Example/ReminderNotification.cs
Normal file
13
patterns/bridge/Bridge Example/ReminderNotification.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
namespace Bridge_Example;
|
||||
|
||||
//This is one of the refined abstraction classes. Note that it can make use of _sender since it inherits it from its superclass. Note also that since EmailSender and SmsSender implement IMessageSender, their specific SendMessage() method will be called, depending on what's passed into the constructor (see the main method)
|
||||
public class ReminderNotification : Notification
|
||||
{
|
||||
public ReminderNotification(IMessageSender sender)
|
||||
: base(sender) { }
|
||||
|
||||
public override void Notify(string message)
|
||||
{
|
||||
_sender.SendMessage($"Reminder: {message}");
|
||||
}
|
||||
}
|
||||
16
patterns/bridge/Bridge Example/SmsSender.cs
Normal file
16
patterns/bridge/Bridge Example/SmsSender.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
#region
|
||||
|
||||
using System;
|
||||
|
||||
#endregion
|
||||
|
||||
namespace Bridge_Example;
|
||||
|
||||
//This is another concrete implementor class, which implements the implementor interface IMessageSender
|
||||
public class SmsSender : IMessageSender
|
||||
{
|
||||
public void SendMessage(string message)
|
||||
{
|
||||
Console.WriteLine($"SMS sent: {message}");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user