This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
namespace OpenClosedPrincipleExample | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var customerMessages = new List<Message> | |
{ | |
new SendTextMessage(new Customer {Id = 1, FirstName = "Nick", Phone = "1-303-666-9999" }), | |
new SendEmail(new Customer {Id = 2, FirstName = "Billy", Email = "billy@bojenkins.com"}), | |
new SendTextMessage(new Customer {Id = 2, FirstName = "Jenny", Phone = "1-201-867-5309"} ) | |
}; | |
foreach (var customerMessage in customerMessages) | |
{ | |
customerMessage.SendMessage(); | |
} | |
} | |
} | |
public class Customer | |
{ | |
public int Id; | |
public string FirstName; | |
public string LastName; | |
public string Email; | |
public string Phone; | |
} | |
public abstract class Message | |
{ | |
protected Customer Customer { get; private set; } | |
public Message(Customer customer) | |
{ | |
Customer = customer; | |
} | |
public abstract void SendMessage(); | |
} | |
public class SendTextMessage : Message | |
{ | |
public SendTextMessage(Customer customer) | |
: base(customer) | |
{ | |
} | |
public override void SendMessage() | |
{ | |
//if there is a business rule for validation place here.. | |
Console.WriteLine($"Sending {Customer.FirstName} a text message"); | |
} | |
} | |
public class SendEmail : Message | |
{ | |
public SendEmail(Customer customer) | |
: base(customer) | |
{ | |
} | |
public override void SendMessage() | |
{ | |
//same here previous example had validation of email. | |
Console.WriteLine($"Sending {Customer.FirstName} an email"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment