Command mode instance (C #)
Design a bulletin board system module in command mode:
A software company wants to develop a Windows-based announcement board system. The system provides a main menu that contains some menu items (menuitem). You can use the addmenuitem () method of the menu class to add menu items. The main menu item method is click (). Each menu item contains an abstract command class. The specific command classes include opencommand (Open command), createcommand (new command), and editcommand (edit command) the command class has an execute () method used to call methods such as open (), create (), and edit () of the announcement board system interface class (boardscreen. This system is designed using the command mode to reduce the coupling between the menuitem class and the boardscreen class, draw class diagrams and program implementation.
Class diagram:
Class chart description:
As can be seen from the class diagram, the command abstract class implements three specific classes, namely the createcommand class, the editcommand class, And the opencommand class. Then there is an invoker -- menuitem class, the aggreger -- boardscreen class. The program class can be called as a client.
Various source code:
Using system; using system. collections. generic; using system. LINQ; using system. text; using system. threading. tasks; namespace commandmenu {// javaser class public class boardscreen {public void open () {console. writeline ("Open command excute! ");} Public void create () {console. writeline (" CREATE Command excute! ");} Public void edit () {console. writeline (" Edit command excute! ") ;}} // Command class public abstract class command {protected boardscreen; Public command (boardscreen) {This. boardscreen = boardscreen;} Abstract Public void execute () ;}// specific command class public class opencommand: Command {public opencommand (boardscreen): Base (boardscreen) {} public override void execute () {// throw new notimplementedexception (); boardscreen. open () ;}} public class createcommand: Command {public createcommand (boardscreen): Base (boardscreen) {} public override void execute () {// throw new notimplementedexception (); boardscreen. create () ;}} public class editcommand: Command {public editcommand (boardscreen): Base (boardscreen) {} public override void execute () {// throw new notimplementedexception (); boardscreen. edit () ;}// invoker class public class menuitem {private command; Public void addmenuitem (command) {This. command = command;} public void click () {command. execute () ;}// client class program {static void main (string [] ARGs) {boardscreen = new boardscreen (); menuitem = new menuitem (); // use the opencommand class command opencommand = new opencommand (boardscreen); menuitem. addmenuitem (opencommand); menuitem. click (); // use the createcommand class command creatcommand = new createcommand (boardscreen); menuitem. addmenuitem (creatcommand); menuitem. click (); // use the editcommand class command editcommand = new editcommand (boardscreen); menuitem. addmenuitem (editcommand); menuitem. click (); console. readkey ();}}}
Running result diagram:
Intuitive relationship diagram: