这两个文件必须保持一致性,如果有个声明式被改变了,两个文件都得改变。因此一般会有一个#include一个声明文件而不是前置声明若干函数。
像People这样定
1 #include "People.h" 2 #include "PeopleImpl.h" 3 4 People::People(const std::string& name, const Date& brithday, const Image& Img) 5 :pImpl(new PersonImpl(name,brithday,addr)) 6 { } 7 std::string People::name( ) const 8 { 9 return pImpl->name( ); 10 } |
而另外一种Handle类写法是令People成为一种特殊的abstract base class称为Interface类。看到interface这个关键字或许熟悉C#、java的同学可能已经恍然大悟了。这种接口它不带成员变量,也没有构造函数,只有一个virtual析构函数,以及一组纯虚函数,用来表示整个接口。针对People而写的interface class看起来是这样的。
1 class People{ 2 public: 3 virtual ~People( ); 4 virtual std::string name( ) const = 0; 5 virtual Date brithDate( ) const =0; 6 virtual Image address( ) const =0; 7 ... 8 }; |
怎么创建对象呢?它们通常调用一个特殊函数。这样的函数通常称为工厂函数或者虚构造函数。它们返回指针指向动态分配所得对象,而该对象支持interface类的接口。
1 class People { 2 public: 3 ... 4 static People* create(const std::string& name,const Date& brithday, const Image& Img); 5 }; |
支持interface类接口的那个类必须定义出来,而且真正的构造函数必须被调用
1 class RealPeople:public People{ 2 public: 3 RealPeople(const std::string& name,const Date& birthday,const Image& Img) 4 :theName(name),theBrithDate(brithday),theImg(Img) 5 {} 6 virtual ~RealPeople() { } 7 std::string name( ) const; 8 Date birthDate( ) const; 9 Image img( ) const; 10 private: 11 std::string theName; 12 Date theBirthDate; 13 Image theImg; 14 } |
有了RealPeople类,我们People::create可以这样写
1 People* People::create(const std::string& name, const Date& birthday, const Image& Img) 2 { 3 return static_cast<People *>(new RealPerson(name,birthday,Img)); 4 } |
Handle类与interface类解除了接口和实现之间的耦合关系,从而降低了文件间的编译依存性。但同时也损耗了一些性能与空间。