Simple factory mode-MFC calculator implementation (continuous operation)

Source: Internet
Author: User

Simple factory mode-MFC calculator implementation (continuous operation)

The design pattern is the goal of refactoring. It plays an important role in code reuse and expansion. The importance of restructuring and design patterns is self-evident in learning object-oriented models. Then, the learning design model should be combined with software design and software coding, and driven by examples. This article will learn the simple factory mode through the implementation of the MFC calculator.

In any case, we should first abstract the entity class of the MFC calculator. It is the first step to find the entity class accurately by searching for nouns and dynamic nouns.

Obviously, its core entity class is the Operator operation class. Think about it, and its implementation is closely related to a simple factory.

Simple factory mode explanation:

Simple Factory Pattern is an innovative class mode, also known as Static FactoryMethod Pattern. It is used to create instances of other classes by defining a class, the created instance usually has a common parent class.

UML diagram of simple factory mode:

The roles in the simple factory model and their responsibilities are as follows:

Creator: this is the core of the simple factory model. It is responsible for creating the internal logic of all classes. Of course, the factory class must be called by the outside world to create the required product objects.

Abstract Product role: the parent class of all objects created in simple factory mode. Note that the parent class can be an interface or an abstract class, it describes the common public interfaces of all instances.

Concrete Product role: a specific instance object created by a simple factory. These products often share a common parent class.


The design of the calculator naturally comes out. Please refer to the class diagram.


Here, the polymorphism feature of C ++-parent class Object Pointer operation subclass object. The core classes are OperatorFactory and Operator. Parameters are transmitted as strings.

Key code and project sharing are attached below.

// CALCULATOR_MFCDlg.h: header file // # pragma once # include
 
  
# Include "afxwin. h "// # include" Operator. h "# include" OpertorFactory. h "using namespace std; // CCALCULATOR_MFCDlg dialog box class CCALCULATOR_MFCDlg: public CDialogEx {// construct public: CCALCULATOR_MFCDlg (CWnd * pParent = NULL ); // standard constructor // data in the dialog box enum {IDD = IDD_CALCULATOR_MFC_DIALOG}; protected: virtual void DoDataExchange (CDataExchange * pDX); // DDX/DDV supports/protected: HICON m_hIcon; // The generated message ing function virtual BOOL OnInitDialog (); afx_msg void OnSysCommand (UINT nID, LPARAM lParam); afx_msg void OnPaint (); afx_msg HCURSOR OnQueryDragIcon (); revoke () public: afx_msg void Merge (); CEdit m_EditNumA; CEdit m_EditOperator; CEdit m_EditNumB; // CString m_strNumA; double m_NumA; double m_NumB; double m_Num; double m_Result; CString m_strTemp; char m_ton; afx_msg void OnBnClickedButtonpoint (); afx_msg void forward (); afx_msg void OnBnClickedButton2 (); afx_msg void forward (); afx_msg void Merge (); afx_msg void OnBnClickedButton7 (); afx_msg void Merge (); afx_msg void OnBnClickedButtonadd (); afx_msg void OnBnClickedButtonsub (); afx_msg void forward (); afx_msg void OnBnClickedButtondiv (); afx_msg void OnBnClickedButtonclear (); double StrTempToDou (); stack
  
   
S_Num; stack
   
    
S_timeout; // OpertorFactory OF; double GetFinalResult (double a, double B, char op); void CalSNum (); void ExEOperator (char op );};
   
  
 

Void CCALCULATOR_MFCDlg: OnBnClickedButton0 () {// TODO: add the control notification handler code here // CString str = _ T ("0"); // m_EditNumA.GetWindowText (str ); m_strTemp + = _ T ("0"); // m_strNumA + = str; m_EditNumA.SetWindowTextW (m_strTemp);} void CCALCULATOR_MFCDlg: OnBnClickedButtonpoint () {// TODO: add the control notification handler code m_strTemp + = _ T (". "); m_EditNumA.SetWindowTextW (m_strTemp);} void CCALCULATOR_MFCDlg: OnBnClickedButtonequal () {// TODO: add control notification handler code here/ /OpertorFactory OF;/* m_NumB = StrTempToDou (); m_strTemp = _ T (""); Operator * Operator = NULL; Operator =. createOperator (m_operator); Handler-> SetNumberA (m_NumA); Handler-> SetNumberB (m_NumB); double result = success-> GetResult (); m_Result = result; // m_NumA = m_Result; m_strTemp.Format (_ T ("%. 2f "), result); m_EditNumA.SetWindowTextW (m_strTemp); m_EditNumB.SetWindowTextW (_ T (" "); */m_Num = StrTempToDou (); m_strTemp = _ T (""); s_Num.push (m_Num);/* doubl E a = S_Num.top (); S_Num.pop (); double B = S_Num.top (); S_Num.pop (); char op = S_Oper.top (); S_Oper.pop (); m_Result = GetFinalResult (, b, op); */CalSNum (); m_strTemp.Format (_ T ("%. 2f "), m_Result); m_EditNumA.SetWindowTextW (m_strTemp); // clearwhile (! S_Num.empty () S_Num.pop (); while (! S_Oper.empty () S_Oper.pop (); m_Result = 0.0;} void CCALCULATOR_MFCDlg: OnBnClickedButton1 () {// TODO: add the control notification handler code m_strTemp + = _ T ("1"); m_EditNumA.SetWindowTextW (m_strTemp);} void CCALCULATOR_MFCDlg: OnBnClickedButton2 () {// TODO: add the control notification handler code m_strTemp + = _ T ("2"); m_EditNumA.SetWindowTextW (m_strTemp);} void CCALCULATOR_MFCDlg: OnBnClickedButton3 () {// TODO: add the control notification handler code m_strTemp + = _ T ("3"); m_EditNumA.SetWindowTextW (m_strTemp);} void CCALCULATOR_MFCDlg: OnBnClickedButton4 () {// TODO: add the control notification handler code m_strTemp + = _ T ("4"); m_EditNumA.SetWindowTextW (m_strTemp);} void CCALCULATOR_MFCDlg: OnBnClickedButton5 () {// TODO: add the control notification handler code m_strTemp + = _ T ("5"); m_EditNumA.SetWindowTextW (m_strTemp);} void CCALCULATOR_MFCDlg: OnBnClickedButton6 () {// TODO: add the control notification handler code m_strTemp + = _ T ("6"); m_EditNumA.SetWindowTextW (m_strTemp);} void CCALCULATOR_MFCDlg: OnBnClickedButton7 () {// TODO: add the control notification handler code m_strTemp + = _ T ("7"); m_EditNumA.SetWindowTextW (m_strTemp);} void CCALCULATOR_MFCDlg: OnBnClickedButton8 () {// TODO: add the control notification handler code m_strTemp + = _ T ("8"); m_EditNumA.SetWindowTextW (m_strTemp);} void CCALCULATOR_MFCDlg: OnBnClickedButton9 () {// TODO: add the control notification handler code m_strTemp + = _ T ("9"); m_EditNumA.SetWindowTextW (m_strTemp);} void CCALCULATOR_MFCDlg: OnBnClickedButtonadd () {// TODO: add the control notification handler code here/* m_NumA = StrTempToDou (); m_strTemp + = _ T ("+"); m_EditNumB.SetWindowTextW (m_strTemp); m_temperature = '+ '; // UpdateData (FALSE); m_strTemp = _ T (""); m_EditNumA.SetWindowTextW (m_strTemp); S. push (m_NumA); if (S. size = 2) {} * // * m_Num = StrTempToDou (); m_strTemp = _ T (""); S_Num.push (m_Num); if (S_Num.size () = 2) {/* double A = S_Num.top (); S_Num.pop (); double B = S_Num.top (); S_Num.pop (); char op = S_Oper.top (); S_Oper.pop (); m_Result = GetFinalResult (A, B, op); CalSNum (); S_Num.push (m_Result); S_Oper.push ('+');} else {// S_Num.push (m_Num ); s_Oper.push ('+');} */ExEOperator ('+');} void CCALCULATOR_MFCDlg: OnBnClickedButtonsub () {// TODO: add the control notification handler code ExEOperator ('-');} void CCALCULATOR_MFCDlg: OnBnClickedButtonmul () {// TODO: add the control notification handler code ExEOperator ('*');} void CCALCULATOR_MFCDlg: OnBnClickedButtondiv () {// TODO: add the control notification handler code ExEOperator ('/');} void CCALCULATOR_MFCDlg: OnBnClickedButtonclear () {// TODO: add the control notification handler code // m_strTemp + = _ T (". "); m_EditNumA.SetWindowTextW (_ T (" ");} double CCALCULATOR_MFCDlg: StrTempToDou () {double Num = 0.0; // = atof (m_strTemp ); // m_strTemp.Format (_ T ("%. 2f "), Num); Num = _ ttof (m_strTemp); // m_strTemp = _ T (" "); return Num;} double CCALCULATOR_MFCDlg: GetFinalResult (double, double B, char op) {OpertorFactory OF; Operator * Operator = NULL; Operator =. createOperator (op); operator-> SetNumberA (a); operator-> SetNumberB (B); double result = counter-> GetResult (); return result;} void CCALCULATOR_MFCDlg :: calSNum () {double A = S_Num.top (); S_Num.pop (); double B = S_Num.top (); S_Num.pop (); char op = S_Oper.top (); S_Oper.pop (); if (op = '+' | op = '*') m_Result = GetFinalResult (A, B, op); else m_Result = GetFinalResult (B,, op);} void CCALCULATOR_MFCDlg: ExEOperator (char op) {m_Num = StrTempToDou (); m_strTemp = _ T (""); S_Num.push (m_Num); if (S_Num.size () = 2) {/* double A = S_Num.top (); S_Num.pop (); double B = S_Num.top (); S_Num.pop (); char op = S_Oper.top (); S_Oper.pop (); m_Result = GetFinalResult (A, B, op); */CalSNum (); S_Num.push (m_Result); S_Oper.push (op);} else {// S_Num.push (m_Num ); s_Oper.push (op );}}

Download: http://download.csdn.net/detail/greenapple_shan/7735979

References: Cheng Jie's big talk Design Model

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.