Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Command Processor II. 2 Command Processor Example Let's look at a simple example of a command processor using states and cities. Get initial information.

Similar presentations


Presentation on theme: "1 Command Processor II. 2 Command Processor Example Let's look at a simple example of a command processor using states and cities. Get initial information."— Presentation transcript:

1 1 Command Processor II

2 2 Command Processor Example Let's look at a simple example of a command processor using states and cities. Get initial information from XML file. Permit user to add information using text menu based commands. Download example from last class http://www.cse.usf.edu/~turnerr/Object_Oriented_Design/Downloads/ 2011_04_04_Command_Processor/ File Menu.zip http://www.cse.usf.edu/~turnerr/Object_Oriented_Design/Downloads/ 2011_04_04_Command_Processor/ Rename as Command_Processor Expand Build Run

3 Program Running 3

4 4 Command Processor We have a working Menu class. Now let's add the command processor. Will use the Menu class. Text based menu Select one of a small number of choices Choices identified by number

5 5 Menu Hierarchy Initial Command State Menu 1. Select State 2. Add State 3. Quit When user selects 1 Ask state. Remember as selected state. Enter new command state, State Selected. When users selects 2 Get new state info Add state. Enter new command state, State Selected with new state selected. When user selects 3 Update file and terminate.

6 6 Menu Hierarchy State Selected Menu Add City Change State Quit When user selects 1 Get new city info. Add city to currently selected state. Remain in same command state. When user selects 2 Switch to Initial command state. When user selects 3 Update file and terminate.

7 7 Command Processor Display menu for current command state. Get user's selection. Invoke function to perform action for selected command. Update command state.

8 8 Command Processing We have two menus. Command state says which one to use. Command results in: Action Possibly new command state.

9 9 Command Processor Select Menu object based on command state. Use Menu object to get command Execute command. Update command state. Start with stubs for commands. Get the wiring right.

10 10 Command Processor Add new class Command_Processor. The control class for our program. Only purpose is as home for a static method that will be the top level of control. Process_Commands. Define a private default constructor to show the intent that the class should never be instantiated.

11 11 Command_Processor.h #pragma once #include "Menu.h" class Command_Processor { public: static void Process_Commands(); private: enum Command_States { Initial, State_Selected, Done }; static const int NR_STATES = (int) Done + 1; static Command_States command_state; static Menu* menus[NR_STATES]; Command_Processor(void) {}; }; Private constructor ensures that class cannot be instantiated.

12 12 Command_Processor.cpp #include #include "Command_Processor.h" #include "Menu.h" using namespace std; Command_Processor::Command_States Command_Processor::command_state = Initial; Menu* Command_Processor::menus[NR_STATES]; void Command_Processor::Process_Commands() { cout << "Process commands starting\n"; while (command_state != Done) { // Execute next command command_state = Done; } cout << "Command_Processor exiting\n"; } Start with a stub.

13 13 main.cpp #include #include "Command_Processor.h" using namespace std; int main(void) { Command_Processor::Process_Commands(); cout << "Normal Termination\n"; cin.get(); return 0; } Build and run.

14 14 Inital Stub Running

15 15 Command_Processor.cpp void Command_Processor::Process_Commands() { cout << "Process commands starting\n"; Create_Menus(); while (command_state != Done) { const string* cmd = menus[command_state]->Get_Command(); switch (command_state) { case Initial: Process_Command_0(*cmd); break; case State_Selected: Process_Command_1(*cmd); break; case Done: break; // Can't happen } cout << "Command_Processor exiting\n"; }

16 16 Command_Processor.cpp void Command_Processor::Create_Menus() { // Menu for Initial command state Menu* menu = new Menu("Enter command number"); menu->Add_Command("Select State"); menu->Add_Command("Add State"); menu->Add_Command("Quit"); menus[0] = menu; // Menu for State Selected menu = new Menu("Enter command number"); menu->Add_Command("Add City"); menu->Add_Command("Change State"); menu->Add_Command("Quit"); menus[1] = menu; }

17 Initial Command State // Process command in Initial command state void Command_Processor::Process_Command_0(const string& cmd) { if (cmd == "Select State") { // Select state cout << "Select State command\n"; command_state = State_Selected; } else if (cmd == "Add State") { // Add state cout << "Add State command\n"; command_state = State_Selected; } else { cout << "Quit command\n"; command_state = Done; }

18 18 State Selected Command State // Process command in State Selected command state void Command_Processor::Process_Command_1(const string& cmd) { if (cmd == "Add City") { // Add City cout << "Add City command\n"; } else if (cmd == "Change State") { cout << "Change State command\n"; command_state = Initial; } else { cout << "Quit command\n"; command_state = Done; }

19 Update Command_Processor.h #pragma once #include "Menu.h" #include using std::string; class Command_Processor { public: static void Process_Commands(); private: enum Command_States { Initial, State_Selected, Done }; static const int NR_STATES = (int) Done + 1; static Command_States command_state; static Menu* menus[NR_STATES]; Command_Processor(void) {}; static void Create_Menus(); static void Process_Command_0(const string& cmd); static void Process_Command_1(const string& cmd); }; 19

20 20 Running with Command Stubs


Download ppt "1 Command Processor II. 2 Command Processor Example Let's look at a simple example of a command processor using states and cities. Get initial information."

Similar presentations


Ads by Google