1. 程式人生 > >3.13 以類取代類型碼

3.13 以類取代類型碼

應該 表達式 ons rate qdebug 必須 pub void 替換

【1】源代碼

 1 #include <QString>
 2 #include <QDebug>
 3 
 4 class Student
 5 {
 6 public:
 7    Student(int id, QString name, int status)
 8        : m_nID(id)
 9        , m_name(name)
10        , m_status(status)
11    {}
12 
13    int getId()
14    {
15        return m_nID;
16 } 17 void setId(int id) 18 { 19 m_nID = id; 20 } 21 22 QString getName() 23 { 24 return m_name; 25 } 26 void setName(QString name) 27 { 28 m_name = name; 29 } 30 31 int getStatus() 32 { 33 return m_status; 34 }
35 void setStatus(int status) 36 { 37 m_status = status; 38 } 39 40 QString toString() 41 { 42 return QString("Student [id = %1, name = %2, status = %3]") 43 .arg(QString::number(m_nID)).arg(m_name).arg(QString::number(m_status)); 44 } 45 46
public: 47 static const int INVALID; 48 static const int VALID; 49 50 private: 51 int m_nID; 52 QString m_name; 53 int m_status; 54 }; 55 56 const int Student::INVALID = 0; 57 const int Student::VALID = 1; 58 59 void main() 60 { 61 Student *pStudent = new Student(1, "張三", Student::VALID); 62 qDebug() << pStudent->toString(); 63 } 64 65 // run out: 66 /* 67 * "Student [id = 1, name = 張三, status = 1]" 68 */

【2】以類取代類型碼

 1 #include <QString>
 2 #include <QDebug>
 3 
 4 class StatusCode
 5 {
 6 public:
 7     StatusCode(int nCode)
 8         : m_nCode(nCode)
 9     {}
10 
11     int getCode()
12     {
13         return m_nCode;
14     }
15 
16     void setCode(int code)
17     {
18         m_nCode = code;
19     }
20 
21     QString toString()
22     {
23         return QString("StatusCode [code = %1]").arg(QString::number(m_nCode));
24     }
25 
26 private:
27     int m_nCode;
28 };
29 
30 class Student
31 {
32 public:
33    Student(int id, QString name, StatusCode status)
34        : m_nID(id)
35        , m_name(name)
36        , m_status(status)
37    {}
38 
39    int getId()
40    {
41        return m_nID;
42    }
43    void setId(int id)
44    {
45         m_nID = id;
46    }
47 
48    QString getName()
49    {
50        return m_name;
51    }
52    void setName(QString name)
53    {
54        m_name = name;
55    }
56 
57    StatusCode getStatus()
58    {
59        return m_status;
60    }
61    void setStatus(StatusCode status)
62    {
63        m_status = status;
64    }
65 
66    QString toString()
67    {
68         return QString("Student [id = %1, name = %2, status = %3]")
69                 .arg(QString::number(m_nID)).arg(m_name).arg(m_status.toString());
70     }
71 
72 private:
73    int m_nID;
74    QString m_name;
75    StatusCode m_status;
76 };
77 
78 void main()
79 {
80     Student *pStudent = new Student(1, "張三", StatusCode(1));
81     qDebug() << pStudent->toString();
82 }
83 
84 // run out:
85 /*
86  * "Student [id = 1, name = 張三, status = StatusCode [code = 1]]"
87  */

【3】總結

類中有一個數值類型碼,但它並不影響類的行為。以一個新的類替換該數值類型碼。

在使用Replace Type Code with Class (以類取代類型碼)之前,你應該先考慮類型碼的其他替換方式。

只有當類型碼是純粹數據時(也就是類型碼不會在switch語句中引起行為變化時),你才能以類來取代它。

更重要的是:任何switch語句都應該運用Replace Conditional with Polymorphism (以多態取代條件表達式)去掉。

為了進行那樣的重構,你首先必須運用 Replace Type Code with Subclass (以子類取代類型碼)或Replace Type Code with State/Strategy (以狀態策略取代類型碼),把類型碼處理掉。

Good Good Study, Day Day Up.

順序 選擇 循環 總結

3.13 以類取代類型碼