1. Inheritance Inheritance means is-a
struct _List_node_base
{
_List_node_base* _M_next;
_List_node_base* _M_prec;
};
template<typename _Tp>
struct _List_node : public _List_node_base
{
_Tp _M_data;
};
The subclass points to the parent class.
Inheritance can also have public protected private
inheritance. The most valuable point is the use of virtual functions.
The construction and destruction under inheritance.
The destructor of the parent class must be a virtual function.
A good habit is as long as it is later Will become the parent class, then its destructor needs to be a virtual function not virtual
2. Compound Composition
Represents the has-a
Adapter design pattern
template<class T>
//先进先出
class queue{
...
protected:
//两段进出
deque<T> c; //底层容器
public:
//以下完全利用c的操作函数完成
bool empty() const {
return c.empty();}
size_type size() const {
return c.size();}
reference front() {
return c.front();}
reference back() {
return c.back();}
//先出先出,只开放了deque的两个功能
void push(const value_type& x){
c.push_back();}
void pop(){
c.pop_front();}
}
Abstract the Composition relationship.
The construction is from the inside out. First, the default constructor of the deque inside is called. Destruction
is to first destroy itself, and then call the destructor of the deque
Life cycle: at the same time
3. Delegation
Composition by reference.
//file String.hpp
class StringRep;
class String{
public:
String();
String(const char* s);
String(const String& s);
String &operator=(const String& s);
~String();
...
private:
StringRep* rep;//pimpl 存在一个指针指向StringRep
};
At any point in time, StringRep can be used and delegated to StringRep
//file String.cpp
#include "String.hpp"
namespace{
class StringRep{
friend class String;
StringRep(const char* s);
~StringRep();
int count;
char* rep;
};
}
String::String(){
...}
...
The implementation of String on the left is always handed over to StringRep, and it only opens the copy construction assignment
point to implementation pointer to the specific implementation (Handle, Body)
. Firewall]
Life cycle: String first, StringRep only when needed
There are String a; String b; String c;
while pointing to StringRep, StringRep uses reference counting and shares the same Hello
Copy On Write concept
When a wants to change the value in rep, then rep will copy and construct a copy, let a change b separately
, and c still shares rep
4. Construction and destruction under inheritance + composite relationship
5. Delegation + Inheritance
class Subject
{
//真正的data位置
int m_value;
vector<Observer*> m_views; //委托delegation
public:
//注册观察者
void attach(Observer* obs)
{
m_views.push_back(obs);
}
void set_val(int value)
{
m_value = value;
notify();
}
//通知所有观察者,更新value
void notify()
{
for(int i = 0;i < m_views.size();++i)
m_view[i]->update(this,m_value);
}
}
Observer, so that Subject can have multiple Observers
just like ppt
As in the picture above, there are four Observers observing the same rectangle
class Observer
{
public:
virtual void update(Subject* sub,int value)=0;
};//Observer可以被继承
– Learned from Mr. Hou Jie