原文地址:
http://www.cprogramdevelop.com/1827752/
C language the idea of object-oriented software architecture will be how to change it?
Menu menu 2 menu menu menu ..
C language the idea of object-oriented software architecture will be how to change it?
Menu menu 2 menu menu menu ..
Switch the menu to the left and right arrow keys on the keyboard focus when the user focus in a menu, tap OK on the keyboard, the CANCEL button menu corresponding processing function calls the focus. Look at the difference between the following two methods:
* Press the OK button * /
void onOkKey ()
{
* To determine what the focus of the menu, press the OK button, call the appropriate handler function * /
Switch (currentFocus)
{
case menu1;
menu1OnOk ();
break;
case menu2;
menu2OnOk ();
break;
...
}
}
* Press the Cancel key * /
void onCancelKey ()
{
* To determine what the focus of the menu, press the Cancel key, call the appropriate handler function * /
Switch (currentFocus)
{
case menu1:
menu1OnCancel ();
break;
case menu2;
menu2OnCancel ();
break;
...
}
}
Compare the following method:
/ * The menu attributes and operations "Package" * /
typedef struct tagSysMenu is
{
char * Text; / * menu text * /
BYTE XPos; / * x coordinate of the menu on the LCD * /
Byte ypos; / * y-coordinate of the menu on the LCD * /
void (* onOkFun) (); / * on the menu, press the OK key processing function pointer * /
void (* onCancelFun) (); / * on the menu, press the Cancel key processing function pointer * /
} SysMenu, * LPSysMenu;
When we define the menu is simply this:
static SysMenu menu [MENU_NUM] =
{
{
"Menu1", 0, 48, menu1OnOk, menu1OnCancel
}
And
{
Menu2, 7, 48, menu2OnOk menu2OnCancel
}
And
{
"Menu3", 14, 48, menu2OnOk, menu2OnCancel
}
And
...
};
The OK button and the Cancel key processing becomes:
/ * Press the OK button * /
void onOkKey ()
{
menu [currentFocusMenu]. onOkFun ();
}
/ * Press the Cancel key * /
void onCancelKey ()
{
menu [currentFoucusMenu]. onCancelFunI ();
}
Program do not greatly among the flowers, and also look good scalability! We just package object-oriented thinking, let the program structure clear, As a result, almost no need to modify the program in the system to add more menu, press handler for the system remains unchanged.