using System;
namespace AbstractClasses
{
public abstract class Messager
{
protected string _recipient;
protected string _message;
public Messager(string recipient, string message)
{
_recipient = recipient;
_message = message;
}
public void ProcessMessage()
{
LogMessageLength();
// the code from here
var isValid = false;
if (ValidateRecipient())
{
isValid = true;
SendMessage();
}
// to here will be implemented by the concrete classes
if (isValid)
NotifyTheServiceOwner("The message was correctly sent");
else
NotifyTheServiceOwner("The message was NOT sent");
}
protected abstract void SendMessage();
protected abstract bool ValidateRecipient();
private void LogMessageLength()
{
// log the message
Console.WriteLine("Sending a message of size: {0}", _message.Length);
}
private void NotifyTheServiceOwner(string note)
{
// notify the service owner via email
}
}
public class SmsMessager : Messager
{
public SmsMessager(string _recipient, string _message)
: base(_recipient, _message)
{ }
protected override bool ValidateRecipient()
{
// validate the phone number
return true;
}
protected override void SendMessage()
{
// use some SMS service to send the message
}
}
public class EmailMessage : Messager
{
public EmailMessage(string _recipient, string _message)
: base(_recipient, _message)
{ }
protected override bool ValidateRecipient()
{
// validate the email address
return true;
}
protected override void SendMessage()
{
// use the .Net classe to send an email
}
}
class Program
{
static void Main(string[] args)
{
var messagers = new Messager[]
{
new EmailMessage("adrian@domain.com","Hello"),
new SmsMessager("+4074312122", "Hi"),
new EmailMessage("rahul@domain2.com", "Hello rahul")
};
foreach (var messager in messagers)
{
messager.ProcessMessage();
}
}
}
}
new Messager[], can abstract class be instantiated?
ReplyDeletenew Messager[] is an array
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDelete