• 将文件间的编译依赖关系(Compilation dependency)降至最低


    //如果如下头文件中有任何一个改变,或者这些头文件所依赖的其他头文件有任何改变,那么每一个#include "Person.h"就得重新编译,任何使用Person Class的文件也必须重新编译。

    ==handle class
    #include
    #include “date.h”
    #include “address.h”
    [Person.h更改前]
    Person {
    public:
    Person(const std::string& name, const Date& birthday, const Address& addr);
    std::string name() const;
    std::string birthday() const;
    std::string address() const;
    private:
    std::string theName;
    Date theBirthDate;
    Address theAddress;
    };

    [Person.h更改后] 这样更改后#include "Person.h"的文件,不会因为PersonImpl, Date, Address的改变而重新编译,因为客户include "Person.h"文件中没有关于PersonImpl, Date, Address它们的定义式include “PersonImpl.h”,而是用了类的声明式class PersonImpl;
    #include
    class PersonImpl; //类的声明式
    class Date;
    class Address;

    class Person {
    public:
    Person(const std::string& name, const Date& birthday, const Address& addr);
    std::string name() const;
    std::string birthdayDate() const;
    std::string address() const;
    private:
    std::tr1:shared_ptr pImpl;
    };

    [Person.cpp]
    #include “Person.h”
    #include “PersonImpl.h”

    Person::Person(const std::string& name, const Date& birthday, const Address& addr)
    pImpl(new PersonImpl(name, birthday, addr))
    ()
    std::string Person::name() const
    {
    return pImpl->name();
    }

    interface class=
    class Person {
    public:
    virtual ~Person();
    virtual std::string name() const=0;
    virtual std::string birthDate() const=0;
    virtual std::string address() const=0;
    static std::tr1::shared_ptr create(const std::string& name, const Date& birthday, const Address& addr);
    };

    客户会这样使用它们:
    std::string name;
    Date dateOfBirth;
    Address address;
    std::tr1::shared_ptr pp(Person::create(name, address, address));
    std::out << pp->name()
    << “was born on "
    << pp->birthDate()
    << " and now lives at”
    << pp->adress();

    class RealPerson : public Person {
    public:
    RealPerson(const std::string& name, const Date& birthday, const Address& addr)
    : theName(name), theBirthDate(birthday), theAddress(addr)
    ()
    virtual ~RealPerson() {}
    std::string name() const;
    std::string birthdayDate() const;
    std::string address() const;
    private:
    std::string theName;
    Date theBirthDate;
    Address theAddress;
    };

    std::tr1::shared_ptr Person::create(const std::string& name, const Date& birthday, const Address& addr);
    {
    return std::tr1::shared_ptr(new RealPerson(name, birthday, addr));
    }

    支持“编译依存性最小化”的一般构想是:相依于声明式,不要相依于定义式。
    程序库头文件应该以“完全且仅有声明式”的形式存在。

  • 相关阅读:
    java惠购网站计算机毕业设计MyBatis+系统+LW文档+源码+调试部署
    8 RESTful案例
    CesiumJS 2022^ 源码解读[0] - 文章目录与源码工程结构
    【电子通识】为什么高速信号传输要用差分
    llama-7B、vicuna-7b-delta-v1.1和vicuna-7b-v1.3——使用体验
    算法训练营day44|动态规划 part06:完全背包 (完全背包、 LeetCode518. 零钱兑换 II、377. 组合总和 Ⅳ )
    下单小程序怎么做呢?
    LeetCode437:路径总和III
    2021年阿里巴巴Java岗位面试(已拿offer)
    java计算机毕业设计校园代购服务订单管理系统源码+系统+mysql数据库+lw文档+部署
  • 原文地址:https://blog.csdn.net/winneyDD/article/details/126007812