Refactoring to Command
1.
using System;
using System.Collections.Generic;
using System.Text;
namespace Refactor2Command
{
public class Mail
{
private string sender;
public string Sender
{
get { return sender; }
set { sender = value; }
}
private string receiver;
public string Receiver
{
get { return receiver; }
set { receiver = value; }
}
private DateTime date;
public DateTime Date
{
get { return date; }
set { date = value; }
}
private string body;
public string Body
{
get { return body; }
set { body = value; }
}
public Mail(string _sender, string _receiver, DateTime _date, string _body)
{
sender = _sender;
receiver = _receiver;
date = _date;
body = _body;
}
}
public enum Request
{
SendMail,
ReplyMail,
ReceiveMail,
ForwardMail
}
public class MailSystem
{
private Mail mail;
public Mail Mail
{
get { return mail; }
set { mail = value; }
}
public void Handle(Request request)
{
LogDown();
if (request == Request.SendMail)
{
Console.WriteLine(string.Format("Send Mail To : {0}",mail.Receiver));
}
else if(request == Request.ReceiveMail)
{
Console.WriteLine(string.Format("Receive Mail From:{0}",mail.Sender));
}
else if (request == Request.ForwardMail)
{
Console.WriteLine(string.Format("Forward {0}'s mail to {1} ",mail.Sender,mail.Receiver));
}
else if (request == Request.ReplyMail)
{
Console.WriteLine(string.Format("Reply {0}'s mail on Date:{1}",mail.Sender,mail.Date));
}
}
private void LogDown()
{
Console.WriteLine(string.Format("Mail Sender:{0}", mail.Sender));
Console.WriteLine(string.Format("Mail Receiver:{0}", mail.Receiver));
Console.WriteLine(string.Format("Mail Date:{0}", mail.Date));
Console.WriteLine(string.Format("Mail Body:{0}", mail.Body));
}
}
}
2.简单重构后:
public class MailSystem
{
private Mail mail;
public Mail Mail
{
get { return mail; }
set { mail = value; }
}
public void Handle(Request request)
{
LogDown();
if (request == Request.SendMail)
{
SendMail();
}
else if(request == Request.ReceiveMail)
{
ReceiveMail();
}
else if (request == Request.ForwardMail)
{
ForwardMail();
}
else if (request == Request.ReplyMail)
{
ReplyMail();
}
}
private void ReplyMail()
{
Console.WriteLine(string.Format("Reply {0}'s mail on Date:{1}", mail.Sender, mail.Date));
}
private void ForwardMail()
{
Console.WriteLine(string.Format("Forward {0}'s mail to {1} ", mail.Sender, mail.Receiver));
}
private void ReceiveMail()
{
Console.WriteLine(string.Format("Receive Mail From:{0}", mail.Sender));
}
private void SendMail()
{
Console.WriteLine(string.Format("Send Mail To : {0}", mail.Receiver));
}
private void LogDown()
{
Console.WriteLine(string.Format("Mail Sender:{0}", mail.Sender));
Console.WriteLine(string.Format("Mail Receiver:{0}", mail.Receiver));
Console.WriteLine(string.Format("Mail Date:{0}", mail.Date));
Console.WriteLine(string.Format("Mail Body:{0}", mail.Body));
}
}
}
3.封装了命令
public class MailSystem
{
private Mail mail;
public Mail Mail
{
get { return mail; }
set { mail = value; }
}
public void Handle(Request request)
{
LogDown();
if (request == Request.SendMail)
{
new SendMailCommand().Handle(mail);
}
else if(request == Request.ReceiveMail)
{
new ReceiveMailCommand().Handle(mail);
}
else if (request == Request.ForwardMail)
{
new ForwardMailCommand().Handle(mail);
}
else if (request == Request.ReplyMail)
{
new ReplyMailCommand().Handle(mail);
}
}
private void LogDown()
{
Console.WriteLine(string.Format("Mail Sender:{0}", mail.Sender));
Console.WriteLine(string.Format("Mail Receiver:{0}", mail.Receiver));
Console.WriteLine(string.Format("Mail Date:{0}", mail.Date));
Console.WriteLine(string.Format("Mail Body:{0}", mail.Body));
}
}
public class SendMailCommand
{
public void Handle(Mail mail)
{
Console.WriteLine(string.Format("Send Mail To : {0}", mail.Receiver));
}
}
public class ReplyMailCommand
{
public void Handle(Mail mail)
{
Console.WriteLine(string.Format("Reply {0}'s mail on Date:{1}", mail.Sender, mail.Date));
}
}
public class ForwardMailCommand
{
public void Handle(Mail mail)
{
Console.WriteLine(string.Format("Forward {0}'s mail to {1} ", mail.Sender, mail.Receiver));
}
}
public class ReceiveMailCommand
{
public void Handle(Mail mail)
{
Console.WriteLine(string.Format("Receive Mail From:{0}", mail.Sender));
}
}
4.最后的重构:
public class MailSystem
{
private Mail mail;
public Mail Mail
{
get { return mail; }
set { mail = value; }
}
private Hashtable cmdList = new Hashtable();
public MailSystem()
{
InitCommand();
}
private void InitCommand()
{
cmdList.Add(Request.ForwardMail,new ForwardMailCommand());
cmdList.Add(Request.ReceiveMail, new ReceiveMailCommand());
cmdList.Add(Request.ReplyMail, new ReplyMailCommand());
cmdList.Add(Request.SendMail, new SendMailCommand());
}
public void Handle(Request request)
{
LogDown();
((ICommand)cmdList[request]).Handle(mail);
}
private void LogDown()
{
Console.WriteLine(string.Format("Mail Sender:{0}", mail.Sender));
Console.WriteLine(string.Format("Mail Receiver:{0}", mail.Receiver));
Console.WriteLine(string.Format("Mail Date:{0}", mail.Date));
Console.WriteLine(string.Format("Mail Body:{0}", mail.Body));
}
}
public interface ICommand
{
void Handle(Mail mail);
}
public class SendMailCommand:ICommand
{
public void Handle(Mail mail)
{
Console.WriteLine(string.Format("Send Mail To : {0}", mail.Receiver));
}
}
public class ReplyMailCommand:ICommand
{
public void Handle(Mail mail)
{
Console.WriteLine(string.Format("Reply {0}'s mail on Date:{1}", mail.Sender, mail.Date));
}
}
public class ForwardMailCommand:ICommand
{
public void Handle(Mail mail)
{
Console.WriteLine(string.Format("Forward {0}'s mail to {1} ", mail.Sender, mail.Receiver));
}
}
public class ReceiveMailCommand:ICommand
{
public void Handle(Mail mail)
{
Console.WriteLine(string.Format("Receive Mail From:{0}", mail.Sender));
}
}
5.Client:
public class Client
{
public static void Main()
{
MailSystem sys = new MailSystem();
sys.Mail = new Mail("flyingmain@gmail.com", "f.chen@cleversoft.com", DateTime.Now.Date, "Happy new year !");
sys.Handle(Request.SendMail);
Console.WriteLine("==================================================================");
sys.Handle(Request.ForwardMail);
Console.WriteLine("==================================================================");
sys.Handle(Request.ReceiveMail);
Console.WriteLine("==================================================================");
sys.Handle(Request.ReplyMail);
Console.Read();
}
}
posted on 2007-02-05 22:35 flyingchen 阅读(370) 评论(0) 编辑 收藏 举报