什么是单例:有些时候,我们需要整个程序中有且仅有一个实例
实现思路
Singleton.h
#pragma once
#include
using namespace std;
class Singleton
{
public:
static const Singleton* getInstance();
static void DoSomething()
{
cout << "Do Something" << endl;
}
private:
// 将构造和析构函数私有化,防止外部访问
Singleton();
~Singleton();
static Singleton* This; // 使用静态变量帮助解决资源的分配和释放
};
Singleton.cpp:这里如果将
Singleton* Singleton::This = nullptr;
修改为Singleton* Singleton::This = new Singleton();
就是饿汉式单例
#include "Singleton.h"
Singleton* Singleton::This = nullptr;
const Singleton* Singleton::getInstance()
{
if (!This)
{
This = new Singleton;
}
return This;
}
Singleton::Singleton()
{
}
Singleton::~Singleton()
{
}
Test.cpp
#include
#include "Singleton.h"
using namespace std;
int main()
{
Singleton::getInstance()->DoSomething();
Singleton::getInstance()->DoSomething();
return 0;
}
Observer.h
#pragma once
# ifndef OBSEVER_H_1
# define OBSEVER_H_1
class Observer
{
public:
Observer() { ; }
virtual ~Observer() { ; }
// 当被观察对象发生变化时,通知被观察者调用这个方法
virtual void Update(void* pArg) = 0;
};
# endif
Observerable.h
#pragma once
class Observer;
#include
#include
#include "Observer.h"
using namespace std;
class Observerable
{
public:
Observerable();
virtual ~Observerable();
// 注册观察者
void Attach(Observer* pOb);
// 反注册观察者
void Detach(Observer* pOb);
int GetObseverCount() const
{
return _Obs.size();
}
void DetachAll()
{
_Obs.clear();
}
virtual void GetSomeNews(string str)
{
SetChange(str);
}
protected:
void SetChange(string news); // 有变化,需要通知
private:
void Notify(void* pArg);
private:
bool _bChange; //表示当前状态
list<Observer*> _Obs;
};
Observerable.cpp
#include "Observerable.h"
Observerable::Observerable() :_bChange(false)
{
}
Observerable::~Observerable()
{
}
// 注册观察者
void Observerable::Attach(Observer* pOb)
{
if (pOb == NULL)
{
return;
}
// 看看当前列表中是否有这个观察者
auto it = _Obs.begin();
for (; it != _Obs.end(); it++)
{
if (*it == pOb)
{
return;
}
}
_Obs.push_back(pOb);
}
// 反注册观察者
void Observerable::Detach(Observer* pOb)
{
if ((pOb == NULL) || (_Obs.empty() == true))
{
return;
}
_Obs.remove(pOb);
}
void Observerable::SetChange(string news)
{
_bChange = true;
Notify(((void*)news.c_str()));
}
void Observerable::Notify(void* pArg)
{
if (_bChange == false)
{
return;
}
// 看看当前列表中是否有这个观察者
auto it = _Obs.begin();
for (; it != _Obs.end(); it++)
{
(*it)->Update(pArg);
}
_bChange = false;
}
Test.cpp
#include
#include "Observer.h"
#include "Observerable.h"
using namespace std;
//这个是被观察者
class News : public Observerable
{
public:
virtual void GetSomeNews(string str)
{
SetChange("News: " + str);
}
};
//观察者1
class User1 :public Observer
{
public:
virtual void Update(void* pArg)
{
cout << "User1 Got News: " << reinterpret_cast<char*>(pArg) << endl;
}
};
//观察者2
class User2 :public Observer
{
public:
virtual void Update(void* pArg)
{
cout << "User2 Got News: " << reinterpret_cast<char*>(pArg) << endl;
}
};
int main()
{
User1 u1;
User2 u2;
News n1;
n1.GetSomeNews("T0");
cout << n1.GetObseverCount() << endl; // 0
n1.Attach(&u1);
n1.Attach(&u2);
n1.GetSomeNews("T1");
cout << n1.GetObseverCount() << endl; // 2
n1.Detach(&u2);
n1.GetSomeNews("T2");
cout << n1.GetObseverCount() << endl; // 1
n1.DetachAll();
n1.GetSomeNews("T3");
cout << n1.GetObseverCount() << endl; // 0
return 0;
}
#include
using namespace std;
class LegacyRectangle
{
public:
LegacyRectangle(double x1, double y1, double x2, double y2)
{
_x1 = x1;
_y1 = y1;
_x2 = x2;
_y2 = y2;
}
void LegacyDraw()
{
cout << "LegacyRectangle:: LegacyDraw()" << _x1 << " " << _y1 << " " << _x2 << " " << _y2 << endl;
}
private:
double _x1;
double _y1;
double _x2;
double _y2;
};
class Rectangle
{
public:
virtual void Draw(string str) = 0;
};
// 第一种适配的方式:使用多重继承
class RectangleAdapter : public Rectangle, public LegacyRectangle
{
public:
RectangleAdapter(double x, double y, double w, double h) :
LegacyRectangle(x, y, x + w, y + h)
{
cout << "RectangleAdapter(int x, int y, int w, int h)" << endl;
}
virtual void Draw(string str)
{
cout << "RectangleAdapter::Draw()" << endl;
LegacyDraw();
}
};
int main(void)
{
double x = 20.0, y = 50.0, w = 300.0, h = 200.0;
RectangleAdapter ra(x, y, w, h);
Rectangle* pR = &ra;
pR->Draw("Testing Adapter");
}
#include
using namespace std;
class LegacyRectangle
{
public:
LegacyRectangle(double x1, double y1, double x2, double y2)
{
_x1 = x1;
_y1 = y1;
_x2 = x2;
_y2 = y2;
}
void LegacyDraw()
{
cout << "LegacyRectangle:: LegacyDraw()" << _x1 << " " << _y1 << " " << _x2 << " " << _y2 << endl;
}
private:
double _x1;
double _y1;
double _x2;
double _y2;
};
class Rectangle
{
public:
virtual void Draw(string str) = 0;
};
// 组合方式的Adapter
class RectangleAdapter2 :public Rectangle
{
public:
RectangleAdapter2(double x, double y, double w, double h) :
_lRect(x, y, x + w, y + h)
{
cout << "RectangleAdapter2(int x, int y, int w, int h)" << endl;
}
virtual void Draw(string str)
{
cout << "RectangleAdapter2::Draw()" << endl;
_lRect.LegacyDraw();
}
private:
LegacyRectangle _lRect;
};
int main(void)
{
double x = 20.0, y = 50.0, w = 300.0, h = 200.0;
RectangleAdapter2 ra2(x, y, w, h);
Rectangle* pR2 = &ra2;
pR2->Draw("Testing2 Adapter");
}
#include
using namespace std;
template<class T>
T getMax(T a, T b)
{
return a > b ? a : b;
}
int main(void)
{
// 模板函数的测试
cout << getMax(1, 2) << endl;
cout << getMax(1.5, 3.5) << endl;
cout << getMax('a', 'b') << endl; //使用ASCII大小来比较
cout << getMax("hello", "world") << endl;//hello
}
#include
using namespace std;
template<class T>
T getMax(T a, T b)
{
return a > b ? a : b;
}
//特化
template<>
char* getMax(char* a, char* b)
{
return (strcmp(a, b) > 0 ? (a) : (b));
}
//类型不一致的特化
template<class T1, class T2>
int getMax(T1 a, T2 b)
{
return static_cast<int>(a > b ? a : b);
}
int main(void)
{
// 模板函数的测试
cout << getMax(1, 2) << endl;
cout << getMax(1.5, 3.5) << endl;
cout << getMax('a', 'b') << endl; //使用ASCII大小来比较
cout << getMax("hello", "world") << endl;
const char* s1 = "hello";
const char* s2 = "world";
cout << getMax(const_cast<char*>(s1), const_cast<char*>(s2)) << endl;//world
cout << getMax(10, 2.5) << endl;
}
#define NULL ((void*)0)
#ifndef NULL
#ifdef __cplusplus
#define NULL 0
#else
#define NULL((void*)0)
#endif
#endif
#include
using namespace std;
void func(void* i)
{
cout << "func(void* i)" << endl;
}
void func(int i)
{
cout << "func(int i)" << endl;
}
int main()
{
int* pi = NULL;
int* pi2 = nullptr;
char* pc = NULL;
char* pc2 = nullptr;
func(NULL); // func(int i)
func(nullptr); // func(void* i)
func(pi); // func(void* i)
func(pi2); // func(void* i)
func(pc); // func(void* i)
func(pc2); // func(void* i)
return 0;
}
double f = 1.0 / 2;
double f = double(1) / double(2)
#include
using namespace std;
int main()
{
/ C++ const_cast
const int a = 10;
//int* pA = &a; //"const int *" 类型的值不能用于初始化 "int *" 类型的实体
int* pA = const_cast<int*>(&a);
*pA = 100;
cout << a << endl; // 10
cout << *pA << endl; // 100
cout << &a << endl;
cout << pA << endl;
return 0;
}
#include
using namespace std;
int Test()
{
return 0;
}
int main()
{
// C++ reinterpret_cast
typedef void(*FuncPtr) ();//这里定义的函数指针返回值是void类型
FuncPtr funcPtr;
//funcPtr = &Test; //不能将 "int (*)()" 类型的值分配到 "FuncPtr" 类型的实体
funcPtr = reinterpret_cast<FuncPtr>(&Test);
return 0;
}
int main()
{
int ii = 5;
double dd = static_cast<double>(ii);//基本类型转换 int -> double
double dd2 = 5.6;
int ii2 = static_cast<int>(dd2);//基本类型转换 double -> int
return 0;
}
#include
using namespace std;
class Base
{
public:
Base() : _i(0) { ; }
virtual void T() { cout << "Base:T" << _i << endl; }
private:
int _i;
};
class Derived : public Base
{
public:
Derived() :_j(1) { ; }
virtual void T() { cout << "Derived:T" << _j << endl; }
private:
int _j;
};
int main()
{
Base cb;
Derived cd;
Base* pcb;
Derived* pcd;
// 子类--》 父类(向上)
pcb = static_cast<Base*>(&cd);
if (pcb == NULL)
{
cout << "unsafe static_cast from Derived to Base" << endl;
}
pcb = dynamic_cast<Base*>(&cd);
if (pcb == NULL)
{
cout << "unsafe dynamic_cast from Derived to Base" << endl;
}
// 父类--》 子类(向下)
pcd = static_cast<Derived*>(&cb);
if (pcd == NULL)
{
cout << "unsafe static_cast from Derived to Base" << endl;
}
pcd = dynamic_cast<Derived*>(&cb);
if (pcd == NULL)
{
cout << "unsafe dynamic_cast from Derived to Base" << endl;
}
return 0;
}