大型C++軟體設計


[定義: Uses-In-The-Implementation]

   名稱       意義
   Uses      該class有一個成員函式使用了該型別
   HasA      該class嵌入了一個該型別的物件實體
   HoldsA    該class嵌入了一個該型別的指標(或引用)
   WasA      該class private繼承於該型別


[Uses]

     class Crook
     {
     private:
        void bribe();
     };

     class Judge;

     void Crook::bridge()
     {
        Judge *bad = 0;
     };

[HasA 與 HoldsA]

     class Tower{};
     class Cannon;

     class BattleShip{
        Tower d_controlTower;
        Cannon *d_replaceableForwardBattery_p;
        Cannon& d_fixedAftBattery;
     };

[WasA]
 
     class Battleship{};
     class Shop{};
     class Exhibit;

     class ArizonaMemorial:private Battleship{
       Shop d_giftShop;
       Exhibit *d_current_p;
       Exhibit& d_default;
     };


Member Data Access
主要設計規則::
務必將class的成員變數宣告為private
如果其中一個class定義了public成員變數而在許多程式被使用,那麼一旦改變了該class成員變數的型態或格式,則那些程式必須大幅修改而且必須整個重新編譯

主要設計規則:
避免在檔案範圍內使用具外部連結性的資料

全域變數局部化:
1.將所有全域變數放到一個struct或class裡
2.接著將它們宣告為private並提供一個static成員函式供使用者存取

class Global { static int s_size; static double s_scale; static const char *s_system; private: Global(); public: static void setSize(int size); static void setScale(double scale); static void setSystem(const char *system){s_system = system;} static int getSize(){return s_size;} static double getScale(){return s_scale;} static const char *getSystem(){return s_system;) };

全域存取的模組在當它們相關於個別繼承的物件時,或是一個作用範圍涵蓋整個系統,但未被某一特定應用程式所控制的常數來說,此全域存取的模組是有意義的


主要設計規則:
避免在表頭檔中使用除了引入保護之外的前處理器巨集
次要設計規則:
在每一個表頭檔的前置處理器的引入敘述前後,放置一個多餘(外部)引入保護的檢查機制


#ifndef INCLUDE_S13
#define INCLUDE_S13

#ifndef INCLUDE_W1
#include "w1.h"
#endif

#ifndef INCLUDE_W2
#include "w2.h"
#endif

#ifndef INCLUDE_W3
#include "w3.h"
#endif

//...

#ifndef INCLUDE_WN
#include "wn.h"
#endif

#ifndef INCLUDE_MATH
#include math.h
#define INCLUDE_MATH
#endif


class S13
{
   W1 d_w1a;
   W1 d_w1b;
   W2 d_w2;
   W3 d_w3;
   //...
   Wn d_wn;
};
#endif

留言

  1. 請問一下
    這本書您讀起來順嘛?翻譯的好嗎?

    回覆刪除
  2. 回覆
    1. 是喔
      對您有幫助嗎?其實我是作者啦,有批評不要客氣ㄋㄟ

      刪除

張貼留言

這個網誌中的熱門文章

Json概述以及python對json的相關操作

Docker容器日誌查看與清理

遠程控制管理工具ipmitool