QT之計算器代碼重構
阿新 • • 發佈:2018-03-18
QT 代碼重構
首先我們來講講為什麽要進行代碼重構?在一些大的項目中,代碼重構是一個必不可少的步驟。因為項目大了,代碼也就多了,後期的維護將會很難,所以我們要適當的進行代碼重構,讓代碼的可復用性提高,使軟件的設計和架構更加合理。
代碼實現和代碼重構有什麽不同呢?代碼實現的重點是功能的實現,而代碼重構則是在實現功能的基礎上進行再次優化,以提高代碼質量。
那麽什麽樣的代碼需要重構呢?依據以往的項目開發經驗來看呢,當發現項目中的重復代碼越來越多時、代碼功能越來越不清晰時、代碼離設計越來越遠時等,那麽這時我們就該進行代碼重構了,使得代碼的質量得到提高。
有的項目也不需要進行代碼重構,比如說項目較緊。這時我們也沒時間去進行代碼重構,那麽我們就只能實現基本功能就行。
好了,廢話不多說,我們擼起袖子開幹了。由於上次我沒有把計算器界面實現的源代碼貼出來,這次就補上,大家在重構完之後可以看看代碼質量是否得到了提高呢?
#include <QApplication>
#include <QLineEdit>
#include <QPushButton>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget* w = new QWidget();
QLineEdit* le = new QLineEdit(w);
QPushButton* button[20] = {0};
const char* btnText[20] =
{
"7", "8", "9", "+", "(",
"4", "5", "6", "-", ")",
"1", "2", "3", "*", "<-",
"0", ".", "=", "/", "C",
};
int ret = 0;
le->move(10, 10);
le->resize(240, 30);
le->setReadOnly(true);
for(int i=0; i<4; i++)
{
for(int j=0; j<5; j++)
{
button[i*5 + j] = new QPushButton(w);
button[i*5 + j]->resize(40, 40);
button[i*5 + j]->move(10 + (10 + 40)*j, 50 + (10 + 40)*i);
button[i*5 + j]->setText(btnText[i*5 + j]);
}
}
w->show();
w->setFixedSize(w->width(), w->height());
ret = a.exec();
delete w;
return ret;
}
我們 首先可以建一個 QCalculatorUI 類,在這個 QCalculatorUI.cpp 文件中實現所有與計算器界面相關的功能,從而 main 函數將會變得很清晰。首先我們在構造函數中得生成 QCalculatorUI 對象,但是由於考慮到生成的對象有可能會是半成品,所以我們采用二階構造設計法來生成 QCalculatorUI 對象,依次來確保生成的對象時可用的。關於二階構造法,是屬於C++裏面的知識,以後有時間給大家再做詳細介紹。我們之後可以將顯示等功能放在一個show函數裏。那麽依照目前的思想,我們建出來的 QCalculatorUI.h 文件和 QCalculatorUI.cpp 文件的相關代碼就分別是這樣的: QCalculatorUI.h class QCalculatorUI : public QWidget { private: QLineEdit* m_edit; QPushButton* m_buttons[20]; QCalculatorUI(); bool construct(); public: static QCalculatorUI* NewInstance(); void show(); ~QCalculatorUI(); }; ----- QCalculatorUI.cpp 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()); } ----- 進行代碼重構之後的main.cpp是這樣的: #include <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; } 那麽大家看看進行代碼重構之後的main函數是不是很清晰呢?所以在進行代碼實現功能的基礎上,大家一定要進行代碼重構。這樣才能寫出質量更高的代碼,好了,本節就到 這了,後面我們再進行計算器的繼續實現。 以上內容來自狄泰軟件學院的QT教程,歡迎大家一起來學習,可以加我QQ:243343083,一起學習。狄泰技術交流群:199546072
QT之計算器代碼重構