Refactoring to command

Source: Internet
Author: User

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 Explorer;

Public String Explorer
{
Get {return aggreger ;}
Set {Cycler = 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 er, datetime _ date, string _ body)
{
Sender = _ sender;
Worker ER = _ worker er;
Date = _ date;
Body = _ body;
}
}

Public Enum request
{
Sendmail,
Replymail,
Receivemail,
Forwardmail
}

Public class mailsystem
{
Private mail;

Public mail
{
Get {return mail ;}
Set {mail = value ;}
}

Public void handle (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 author Er: {0}", mail. Author ER ));
Console. writeline (string. Format ("mail Date: {0}", mail. Date ));
Console. writeline (string. Format ("mail body: {0}", mail. Body ));
}
}
}

2. After simple refactoring:

Public class mailsystem
{
Private mail;

Public mail
{
Get {return mail ;}
Set {mail = value ;}
}

Public void handle (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 author Er: {0}", mail. Author ER ));
Console. writeline (string. Format ("mail Date: {0}", mail. Date ));
Console. writeline (string. Format ("mail body: {0}", mail. Body ));
}
}
}

3. encapsulated commands

Public class mailsystem
{
Private mail;

Public mail
{
Get {return mail ;}
Set {mail = value ;}
}

Public void handle (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 author Er: {0}", mail. Author ER ));
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)
{
Console. writeline (string. Format ("send mail to: {0}", mail. Receiver ));
}
}

Public class replymailcommand
{
Public void handle (mail)
{
Console. writeline (string. Format ("Reply {0}'s mail on Date: {1}", mail. Sender, mail. Date ));
}
}

Public class forwardmailcommand
{
Public void handle (mail)
{
Console. writeline (string. Format ("forward {0}'s mail to {1}", mail. Sender, mail. Receiver ));
}
}

Public class receivemailcommand
{
Public void handle (mail)
{
Console. writeline (string. Format ("receive mail from: {0}", mail. Sender ));
}
}

4. Final reconstruction:

Public class mailsystem
{
Private mail;

Public 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)
{
Logdown ();
(Icommand) cmdlist [request]). Handle (Mail );
}

Private void logdown ()
{
Console. writeline (string. Format ("mail Sender: {0}", mail. Sender ));
Console. writeline (string. Format ("mail author Er: {0}", mail. Author ER ));
Console. writeline (string. Format ("mail Date: {0}", mail. Date ));
Console. writeline (string. Format ("mail body: {0}", mail. Body ));
}
}

Public interface icommand
{
Void handle (mail );
}

Public class sendmailcommand: icommand
{
Public void handle (mail)
{
Console. writeline (string. Format ("send mail to: {0}", mail. Receiver ));
}
}

Public class replymailcommand: icommand
{
Public void handle (mail)
{
Console. writeline (string. Format ("Reply {0}'s mail on Date: {1}", mail. Sender, mail. Date ));
}
}

Public class forwardmailcommand: icommand
{
Public void handle (mail)
{
Console. writeline (string. Format ("forward {0}'s mail to {1}", mail. Sender, mail. Receiver ));
}
}

Public class receivemailcommand: icommand
{
Public void handle (mail)
{
Console. writeline (string. Format ("receive mail from: {0}", mail. Sender ));
}
}

5. Client:

public class client
{< br> Public static void main ()
{< br> 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 ();
}< BR >}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.