09-计算器界面代码重构

1. 重构的概念

重构:

-以改善代码质量为目的的代码重写
	使其软件的设计和架构更加合理
	提高软件的扩展性和维护性

代码实现与代码重构不同:

-代码实现
	按照设计编程实现,重心在于功能实现
-代码重构
	以提高代码质量为目的的软件架构优化

2. 软件开发流程

从工程的角度对软件开发中的活动进行定义和管理

在这里插入图片描述

什么样的代码需要重构:

  • 当发现项目中重复的代码越来越多时
  • 当发现项目中代码功能越来越不清晰时
  • 当发现项目中代码离设计越来越远时

小贴士:重构是维持代码质量在可接受范围内的重要方式,重构的时机和方式由项目组使用的软件开发过程决定

3. 计算器界面代码重构

]

//【QCalculatorUI.h】
#ifndef _QCALCULATORUI_H_
#define _QCALCULATORUI_H_

#include <QWidget>
#include <QLineEdit>
#include <QPushButton>

class QCalculatorUI : public QWidget
{
private:
    QLineEdit* m_edit;
    QPushButton* m_buttons[20];

    QCalculatorUI();
    bool construct();
public:
    static QCalculatorUI* NewInstance();
    void show();
    ~QCalculatorUI();
};

#endif



//【QCalculatorUI.cpp】
#include "QCalculatorUI.h"

QCalculatorUI::QCalculatorUI() : QWidget(NULL, Qt::WindowCloseButtonHint)
{

}

bool QCalculatorUI::construct()
{
    bool ret = true;
    const char* btnText[20] =
    {
        "7", "8", "9", "+", "(",
        "4", "5", "6", "-", ")",
        "1", "2", "3", "*", "<-",
        "0", ".", "=", "/", "C",
    };

    m_edit = new QLineEdit(this);

    if( m_edit != NULL )
    {
        m_edit->move(10, 10);
        m_edit->resize(240, 30);
        m_edit->setReadOnly(true);
    }
    else
    {
        ret = false;
    }

    for(int i=0; (i<4) && ret; i++)
    {
        for(int j=0; (j<5) && ret; j++)
        {
            m_buttons[i*5 + j] = new QPushButton(this);

            if( m_buttons[i*5 + j] != NULL )
            {
                m_buttons[i*5 + j]->resize(40, 40);
                m_buttons[i*5 + j]->move(10 + (10 + 40)*j, 50 + (10 + 40)*i);
                m_buttons[i*5 + j]->setText(btnText[i*5 + j]);
            }
            else
            {
                ret = false;
            }
        }
    }

    return ret;
}

QCalculatorUI* QCalculatorUI::NewInstance()
{
    QCalculatorUI* ret = new QCalculatorUI();

    if( (ret == NULL) || !ret->construct() )
    {
        delete ret;
        ret = NULL;
    }

    return ret;
}

void QCalculatorUI::show()
{
    QWidget::show();

    setFixedSize(width(), height());
}

QCalculatorUI::~QCalculatorUI()
{

}

4. 总结

  • 重构是软件开发中的重要概念
  • 重构是以提高软件质量为目的的软件开发活动
  • 重构不能影响已有的软件功能
  • 当软件功能的实现进行到了一定阶段时就需要考虑重构
  • 重构可简单的理解为对软件系统进行重新架构
发布了61 篇原创文章 · 获赞 31 · 访问量 10万+

猜你喜欢

转载自blog.csdn.net/qq_40794602/article/details/105560596
今日推荐