物理设计原则
原则 | 基本含义 |
---|---|
自满足原则 | 头文件本身是可以编译通过的 |
单一职责原则 | 头文件包含的实体的职责是单一的 |
最小依赖原则 | 绝不包含不必要的头文件 |
最小可见性原则 | 尽量封装隐藏类的成员 |
自满足原则
所有头文件都应该自满足的。看一个具体的示例代码,这里定义了一个TestCase.h
头文件。TestCase
对父类TestLeaf, TestFixture
都存在编译时依赖,但没有包含基类的头文件。
反例:
// cppunit/TestCase.h
#ifndef EOPTIAWE_23908576823_MSLKJDFE_0567925
#define EOPTIAWE_23908576823_MSLKJDFE_0567925
struct TestCase : TestLeaf, TestFixture
{
TestCase(const std::string &name="");
private:
OVERRIDE(void run(TestResult *result));
OVERRIDE(std::string getName() const);
private:
ABSTRACT(void runTest());
private:
const std::string name;
};
#endif
为了满足自满足原则,其自身必须包含其所有父类的头文件。
正例:
// cppunit/TestCase.h
#ifndef EOPTIAWE_23908576823_MSLKJDFE_0567925
#define EOPTIAWE_23908576823_MSLKJDFE_0567925
#include "cppunit/core/TestLeaf.h"
#include "cppunit/core/TestFixture.h"
struct TestCase : TestLeaf, TestFixture
{
TestCase(const std::string &name="");
private:
OVERRIDE(void run(TestResult &result));
OVERRIDE(std::string getName() const);
private:
ABSTRACT(void