Command mode of read design mode

Source: Internet
Author: User
Using System;

/// 1. Easy to design command queues
/// 2. If necessary, you can easily log commands
/// 3. easily undo and redo commands
///
/// Command mode:
/// Split the object requesting an operation from the object that knows how to perform an operation.
/// Note: Generally, a method is called directly through an object or a class,
/// There is a tightly coupled relationship between the behavior requestor and the behavior implementer, that is, the call relationship between A and B)
/// For example, when you need to record the call implementation, undo, and redo operations of the series of actions,
/// Decoupling between these behavior requestor and behavior implementers is required.
/// The command mode gives you an idea: abstract, introduce a third party, and implement decoupling.
/// This is also a common decoupling concept for many design models.
///

Namespace Commandpattern
{
Class Program
{
Static   Void Main ( String [] ARGs)
{
Document Doc =   New Document ();

Command docmd =   New Docommand (DOC );
Command undocmd =   New Undocommand (DOC );
Command redocmd =   New Redocommand (DOC );

Cmdinvoke invoke =   New Cmdinvoke ();

Invoke. addcmd (docmd );
Invoke. addcmd (undocmd );
Invoke. addcmd (redocmd );

Invoke. Execute ();

Console. Read ();
}
}

Public   Abstract   Class Command
{
Public Document Doc;
Public Command (document DOC)
{
This . Doc = Doc;
}

Public   Abstract   Void Executecommand ();
}

Public   Class Docommand: Command
{
Public Docommand (document DOC ): Base (Doc ){}

Public   Override   Void Executecommand ()
{
Doc. Do ();
}
}

Public   Class Undocommand: Command
{
Public Undocommand (document DOC ): Base (Doc ){}

Public   Override   Void Executecommand ()
{
Doc. Undo ();
}
}

Public   Class Redocommand: Command
{
Public Redocommand (document DOC ): Base (Doc ){}

Public   Override   Void Executecommand ()
{
Doc. Redo ();
}
}

Public   Class Cmdinvoke
{
List < Command > Cmdlist =   New List < Command > ();

Public   Void Addcmd (command cmd)
{
Cmdlist. Add (CMD );
}

Public   Void Undocmd (command cmd)
{
Cmdlist. Remove (CMD );
}

Public   Void Execute ()
{
Foreach (Command cmd In Cmdlist)
{
Cmd. executecommand ();
}
}
}


Public   Class Document
{
Public   String Info { Set ; Get ;}

Public   Void Do ()
{
Console. writeline ( " Do something " );
}

Public   Void Undo ()
{
Console. writeline ( " Undo something " );
}

Public   Void Redo ()
{
Console. writeline ( " Redo something " );
}
}
}

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.