Context ):
1. The concretestrategy algorithm is required.
2. Maintain a strategy instance internally.
3. dynamically sets the specific implementation algorithm of strategy during runtime.
4. Interaction and data transmission with strategy.
Strategy ):
1. A public interface is defined. Different algorithms implement this interface in different ways. The context uses this interface to call different algorithms, which are generally implemented using interfaces or abstract classes.
Concretestrategy (specific policy class ):
2. Implemented the interface defined by strategy and provided specific algorithm implementation.
UML class diagram:
The following is an example of how to implement the key class code:
// Strategy_mfcdlg.cpp: implementation file // # include "stdafx. H "# include" strategy_mfc.h "# include" strategy_mfcdlg.h "# include" afxdialogex. H "# ifdef _ debug # define new debug_new # endif // class caboutdlg: Public cdialogex {public: caboutdlg () in the caboutdlg dialog box for the" about "menu item of the application (); // dialog box data Enum {IDD = idd_aboutbox}; protected: Virtual void dodataexchange (cdataexchange * PDX); // DDX/DDV support // protected: declare_message_map ()}; cab Outdlg: caboutdlg (): caboutdlg: IDD) {} void caboutdlg: dodataexchange (cdataexchange * PDX) {cdialogex: dodataexchange (PDX);} Export (caboutdlg, cdialogex) end_message_map () // dialog box cstrategy_mfcdlg: Convert (cwnd * pparent/* = NULL */): cdialogex (partition: IDD, pparent) {m_hicon = afxgetapp () -> loadicon (idr_mainframe);} void cstrategy_mfcdlg: dodataexchange (Cdataexchange * PDX) {region: dodataexchange (PDX); ddx_control (PDX, idc_edit_price, m_edit_price); ddx_control (PDX, cost, m_editnum); ddx_control (PDX, idc_edit_show, m_editshow ); ddx_control (PDX, idc_combo1, m_comboboxcomp); ddx_control (PDX, idc_edit_total, m_edittotal);} partition (partition, cdialogex) on_wm_syscommand () on_wm_paint () aggregate () Aggregate Ure, & strong: Strong) on_bn_clicked (idc_button_clear, & strong: onbnclickedbuttonclear) end_message_map () // notify the message processing program bool handle: oninitdialog () {cdigealox :: oninitdialog (); // set "about... "menu items are added to the system menu. // Idm_aboutbox must be in the range of system commands. Assert (idm_aboutbox & 0xfff0) = idm_aboutbox); Assert (idm_aboutbox <0xf000); cmenu * psysmenu = getsystemmenu (false); If (psysmenu! = NULL) {bool bnamevalid; cstring straboutmenu; bnamevalid = straboutmenu. loadstring (ids_aboutbox); Assert (bnamevalid); If (! Straboutmenu. isempty () {psysmenu-> appendmenu (mf_separator); psysmenu-> appendmenu (mf_string, idm_aboutbox, straboutmenu); }}// set the icon of this dialog box. When the main window of the application is not a dialog box, the framework will automatically // execute this operation seticon (m_hicon, true); // set the big icon seticon (m_hicon, false ); // set the small icon // todo: add additional initialization code m_comboboxcomp.addstring (_ T ("normal"); m_comboboxcomp.addstring (_ T ("") here ")); m_comboboxcomp.addstring (_ T (""); Total = 300; return true; // return true} void cstrategy_mfcdlg unless the focus is set to the control :: onsyscommand (uint NID, lparam) {If (NID & 0xfff0) = idm_aboutbox) {caboutdlg dlgabout; dlgabout. d Omodal () ;}else {cdialogex: onsyscommand (NID, lparam) ;}// if you want to add the minimization button to the dialog box, you need the following code // to draw the icon. For MFC applications that use document/view models, // This will be automatically completed by the framework. Void cstrategy_mfcdlg: onpaint () {If (isiconic () {cpaintdc DC (this); // The device context sendmessage (wm_iconerasebkgnd, reinterpret_cast <wparam> (DC. getsafehdc (), 0); // center the icon in the rectangle of the workspace int cxicon = getsystemmetrics (sm_cxicon); int cyicon = getsystemmetrics (sm_cyicon); crect rect; getclientrect (& rect); int x = (rect. width ()-cxicon + 1)/2; int y = (rect. height ()-cyicon + 1)/2; // draw the icon DC. drawicon (X, Y, m_hicon) ;} Else {cdialogex: onpaint () ;}/// the system calls this function to retrieve the cursor when the user drags the minimized window. // display the cursor. Hcursor cstrategy_mfcdlg: onquerydragicon () {return static_cast
References: Cheng Jie's big talk Design Model