Command Pattern

zhaozj2021-02-16  102

Original: http://www.c-sharpcorner.com/code/2002/mar/commandpatternsincscku.asp

// file name: iCommand.csusing system; public interface icommand {void execute ();

// file name: dvdplaycommand.csusing system; public class dvdplaycommand: iCommand {public DVDPLAYCOMMAND () {}

Public void execute () {console.writeline ("DVD Started.");}}

// file name: DVDStopCommand.csusing system; public class dvdstopCommand: iCommand {public dvdstopCommand () {}

Public void execute () {console.writeline ("DVD Stopped.");}}

// file name: vcrplaycommand.csusing system; public class vcrplaycommand: iCommand {public vCrplayCommand () {}

Public void execute () {console.writeline ("vcr started.");}}

// file name: vcrstopCommand.csusing system; public class vcrstopCommand: iCommand {public vcrstopCommand () {}

Public void execute () {console.writeline ("vcr stopped.");}}

// file name: romote.csusing system; public class remote {public remote () {}

Public void invoke (iCommand cmd) {console.writeline ("incoking ..."); cmd.execute ();}}

// File name: client.csusing system; public class client {public client () {}

public static int Main (string [] args) {// Instantiate the invoker object Remote remote = new Remote (); // Instantiate DVD related commands and pass them to invoker object DVDPalyCommand dvdPlayCommand = new DVDPlayCommand (); remote.Invoke (dvdPalyCommand ); DVDStopCommand DvdStopCommand = New DVDStopCommand (); Remote.Invoke (DVDStopCommand);

转载请注明原文地址:https://www.9cbs.com/read-10946.html

New Post(0)