当前位置:网站首页>Qt学习09 计算器界面代码重构
Qt学习09 计算器界面代码重构
2022-06-29 10:30:00 【一个小黑酱】
Qt学习09 计算器界面代码重构
什么是软件开发过程中的重构?
重构的概念
重构 (Refactoring)
以改善代码质量为目的代码重写
- 使其软件的设计和架构更加合理
- 提高软件的扩展性和维护性
重要的差异
代码实现与代码重构不同
代码实现
- 按照设计编写实现,重心在于功能实现
代码重构
- 以提高代码质量为目的的软件架构优化
区别:
- 代码实现不考虑架构的好坏,只考虑功能的实现
- 代码重构时不能影响已实现的功能,只考虑架构的改善
如何开发软件?
软件开发过程
从工程的角度对软件开发中的活动进行定义和管理

值得思考的问题
- 什么样的代码需要重构?
- 当发现项目中重复的代码越来越多时
- 当发现项目中代码功能越来越不清晰时
- 当发现项目中代码离设计越来越远时
重构的进行
重构是维持代码质量在可接受范围内的重要方式,重构的时机和方式由项目组使用的软件开发过程 (Process) 决定
计算器界面代码重构

/* 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()
{
}
/* main.cpp */
#include <QtGui/QApplication>
#include "QCalculatorUI.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QCalculatorUI* cal = QCalculatorUI::NewInstance();
int ret = -1;
if( cal != NULL )
{
cal->show();
ret = a.exec();
delete cal;
}
return ret;
}
小结
- 重构是软件开发中的重要概念
- 重构是以提高代码质量为目的的软件开发活动
- 重构不能影响已有的软件功能
- 当软件功能的实现进行到了一定阶段时就需要考虑重构
- 重构可简单的理解为对软件系统进行重新架构
边栏推荐
- Bs-gx-017 online examination management system based on SSM
- 【每日3题(2)】生成交替二进制字符串的最少操作数
- Easydss is deployed on Disk C, and the video playback cannot be played normally. How to solve this problem?
- 适合小白的树莓派opencv4.0安装
- 微博评论架构设计
- 软件工程导论——第五章——总体设计
- crypto 1~5
- LeetCode 535 TinyURL的加密与解密[map] HERODING的LeetCode之路
- 中国计算语言学大会、全国知识图谱与语义计算大会赛题火热进行中
- How to obtain method parameter values through WinDbg
猜你喜欢

Modbustcp protocol WiFi wireless learning single channel infrared module (round shell version)

软件工程导论——第五章——总体设计

【各种**问题系列】OLTP和OLAP是啥?

Take another picture of cloud redis' improvement path

直击产业落地!飞桨重磅推出业界首个模型选型工具

math_数学表达式&等式方程的变形&组合操作技巧/手段积累

What are the main factors that affect the heat dissipation of LED packaging?

非凸联合创始人李佐凡:将量化作为自己的终身事业

Hit the industry directly! The first model selection tool in the industry was launched by the flying propeller

【每日3题(3)】重新格式化电话号码
随机推荐
Online sql to htmltable tool
Bs-gx-018 student examination system based on SSM
【无标题】我在密谋一件大事
rxjs Observable 设计原理背后的 Pull 和 Push 思路
How to obtain method parameter values through WinDbg
what? It's amazing that you can read the whole comic book for free. You can't learn to be a money saver together
通过网上进行股票开户安全吗
Cornerstone of efficient remote office: effective communication | community essay solicitation
毕业季·进击的技术er - 职场打工人
(JS) handwritten deep copy
Modbustcp protocol network learning single channel infrared module (double-layer board)
BS-GX-018 基于SSM实现在校学生考试系统
Modbustcp protocol WiFi wireless learning single channel infrared module (round shell version)
Thoroughly understand JVM Series 7: what is the flow process of objects in the generational model?
math_ Mathematical expression & deformation of equation equation & accumulation of combined operation skills / means
Mastering the clever use of some shell wildcards will make us write with half the effort
(JS) status mode
ruoyi框架中添加sharding sphere5.0.0分表(通过spi添加自定义分表策略)
Uboot for embedded driver development -- common command parameters in uboot
重建中国科研自信——2022最新自然指数排行榜(Nature Index 2022 )公布,中国的研究产出增幅最大...