Implementations

  1 #include<vector>
  2 #include<string>
  3 //Provision_26 : Postone variable definitions as long as possible
  4 
  5 //如果定义还没用就抛出异常 需要:构造+析构
  6 
  7 void AB() {
  8     class S
  9     {
 10     public:
 11         S() = default;
 12         S(const int& _i) :i(_i) {}
 13     private:
 14         std::string s;
 15         int i;
 16     };
 17     int f(int i);
 18     auto n = 100;
 19     S A;//method A: 1 构造 + 1 析构 + n 赋值
 20     for (int i = 0;i < n;++i)
 21     {
 22         A = S(i);
 23     }
 24     //method B : n 构造 + n 析构
 25     for (int i = 0;i < n;++i)
 26     {
 27         S B(f(i));
 28     }
 29 }
 30 //当 构造 + 析构  < 赋值 :using B 变量A作用域也大
 31 
 32 //Provision_27 Minimize casting
 33 //避免使用 dynamic_cast
 34 
 35 //问题:只有 base point 要执行 derived class member function
 36 //解决: 在base class 中  virtual derived member function
 37 
 38 class Window
 39 {
 40 public:
 41     virtual void blink() = 0;//闪烁
 42 };
 43 class SpecialWindow :public Window
 44 {
 45 public:
 46     virtual void blink() override
 47     {
 48         //....
 49     };
 50 };
 51 
 52 //Provision_31: Minimize compilation dependencies between files
 53 //问题:class private成员往往依赖头文件(#include"Tree_Note.h") 若头文件修改则导致所有依赖该头文件的实现(Tree_Note type)都重新编译
 54 //解决1 :(Handle classes)point to imolementation :将对象实现细节隐藏于一个指针背后
 55 
 56 //Person.h
 57 
 58 #pragma once
 59 #include<string>
 60 #include<memory>
 61 
 62 class PersonImpl;
 63 class Date;
 64 class Address;
 65 
 66 class Person
 67 {
 68 public:
 69     Person(const std::string& name, const Date& birthday, const Address& addr);
 70     std::string name() const;
 71     std::string birthday() const;
 72     std::string address() const;
 73 private:
 74     std::shared_ptr<PersonImpl> pImpl;
 75 };
 76 
 77 //Person.cpp
 78 
 79 #include"person.h"
 80 #include"PersonImpl.h"
 81 
 82 Person::Person(const std::string& name, const Date& birthday, const Address& addr)
 83     :pImpl(std::make_shared<PersonImpl>(name, birthday, addr)) {}
 84 std::string Person::name() const
 85 {
 86     return pImpl->name();
 87 }
 88 std::string Person::birthday() const
 89 {
 90     return pImpl->birthday();
 91 }
 92 std::string Person::address() const
 93 {
 94     return pImpl->address();
 95 }
 96 
 97 //PersonImpl.h
 98 
 99 #pragma once
100 #include<string>
101 #include"Date.h"
102 #include"Address.h"
103 class PersonImpl
104 {
105 public:
106     PersonImpl(const std::string& name, const Date& birthday, const Address& addr)
107         :theName(name), theBirthday(birthday), theAddress(addr) {}
108     std::string name() const;
109     std::string birthday() const;
110     std::string address() const;
111 private:
112     std::string theName;
113     Date theBirthday;
114     Address theAddress;
115 };
116 
117 
118 //PersonImpl.cpp
119 
120 #include"PersonImpl.h"
121 std::string
122 PersonImpl::name() const { return theName; }
123 std::string
124 PersonImpl::birthday() const { return theBirthday.date(); }
125 std::string
126 PersonImpl::address() const { return theAddress.addr(); }
127 
128 
129 //解决2: (Interface class)abstract base 接口在Person 细节在class RealPerson :public Person private
130 //使用:auto pp(Person::create(name,birthday,addr))
131 
132 //person.h
133 
134 class Person
135 {
136 public:
137     virtual ~Person() {}
138     virtual std::string name() const = 0;
139     virtual std::string birthday() const = 0;
140     virtual std::string address() const =0 ;
141 
142     static std::shared_ptr<Person> create(const std::string& name, const Date& birthday, const Address& addr);
143 };
144 
145 
146 //person,cpp
147 
148 #include"Person.h"
149 #include"RealPerson.h"
150 std::shared_ptr<Person>
151 Person::create(const std::string& name, const Date& birthday, const Address& addr)
152 {
153     return std::make_shared<Person>(RealPerson(name, birthday, addr));
154 }
155 
156 
157 //RealPerson
158 
159 #pragma once
160 #include"person.h"
161 #include<string>
162 #include"Date.h"
163 #include"Address.h"
164 
165 class RealPerson :public Person
166 {
167 public:
168     RealPerson(const std::string& name, const Date& birthday, const Address& addr)
169         :theName(name), theBirthday(birthday), theAddress(addr) {}
170     virtual ~RealPerson() {}
171     std::string name() const override;
172     std::string birthday() const override;
173     std::string address() const override;
174 private:
175     std::string theName;
176     Date theBirthday;
177     Address theAddress;
178 };
179 
180 
181 //RealPerson.cpp
182 
183 #include"PersonImpl.h"
184 std::string 
185 PersonImpl::name() const { return theName; }
186 std::string 
187 PersonImpl::birthday() const { return theBirthday.date(); }
188 std::string 
189 PersonImpl::address() const { return theAddress.addr(); }

 

posted @ 2020-12-19 15:32  z974890869  阅读(154)  评论(0编辑  收藏  举报