Tuesday

template method design pattern c#

      
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RulesDemo
{

/* messages */

public class MessageBase
{
public bool ClientActive { get; set; }
}


public class SPQMessage: MessageBase {
public bool Card {get;set;}
}

public class ACPMessage: MessageBase {
public bool URL {get;set;}
}


/* Ruule interface*/

public interface IRule {
string Evaluate (MessageBase messageBase);
string Evaluate (SPQMessage messageBase);
string Evaluate (ACPMessage messageBase);
}


/* rules */

/* rule base */
public class RuleBase: IRule {
public virtual string Evaluate(MessageBase messageBase)
{
throw new NotImplementedException();
}

public virtual string Evaluate(SPQMessage messageBase)
{
return Evaluate((MessageBase)messageBase);
}

public virtual string Evaluate(ACPMessage messageBase)
{
return Evaluate((MessageBase)messageBase);
}
}


/* concrete rules */
public class ClientIsActive : RuleBase
{
public override string Evaluate(MessageBase messageBase)
{
return "ClientIsActive";
}
}

public class UrlIsPresent : RuleBase
{
public override string Evaluate(ACPMessage messageBase)
{
return "UrlIsPresent";
}

}

/* */

class Program
{
static void Main(string[] args)
{

ArrayList list = new ArrayList();
list.Add(new ClientIsActive());
list.Add(new UrlIsPresent());


ACPMessage message = new ACPMessage();
foreach (IRule rule in list)
{
Console.WriteLine(rule.Evaluate(message));
}

/* this will crash because ACP rule doesn't know how to process SPQ message*/
/*
SPQMessage message2 = new SPQMessage();
foreach (IRule rule in list)
{
Console.WriteLine(rule.Evaluate(message2));
}
*/
Console.ReadLine();

}
}
}

No comments:

test smtp server with powershell

Send-MailMessage -SMTPServer smtp.domain.com -To [email protected] -From [email protected] -Subject "This is a test email" -Body ...