Command mode: Encapsulate the request, parameterize the client with different requests, queue the request or log the request, and revoke the operation.
1 namespacedesignmodel. Command mode2 {3 Abstract classCommand4 {5 protectedexecer exe;6 PublicCommand (execer execer)7 {8 This. exe =Execer;9 }Ten Abstract Public voidexeccommand (); One } A - classCommandone:command - { the PublicCommandone (Execer execer):Base(execer) {} - Public Override voidexeccommand () - { - exe. Execcommandone (); + } - } + classCommandtwo:command A { at PublicCommandtwo (Execer execer):Base(execer) {} - Public Override voidExecCommand () =exe. Execcommandtwo (); - - } - - classExecer in { - Public voidExeccommandone () = Console.WriteLine (""); to Public voidExeccommandtwo () = Console.WriteLine (""); + } - the * classBuilder $ {Panax Notoginsengilist<command> list =NewList<command>(); - PublicBuilder setbuilder (Command command) the { + list. ADD (command); A return This; the } + Public voidRemove (Command command) - { $ list. Remove (command); $ } - Public voidNotify () - { theList. All (x = {X.execcommand ();return true; }); - }Wuyi } the } - Static voidCommand mode () Wu { -Execer Execer =Newexecer (); AboutCommand C1 =NewCommandone (execer); $Command C2 =NewCommandtwo (execer); - varBuilder =Newdesignmodel. Command mode. Builder (); - Builder. Setbuilder (C1). Setbuilder (C2). Notify (); -}
View Code
Advantages:
1 command queue can be designed;
2 allow the command receiver to veto the command;
3 Adding a specific command class does not affect other parts.
Design mode 12-command mode