含 typeCode 的单例子类抽取出基类
2022-09-22 本文已影响0人
my_passion
———————————————————————————————————————————— ————————————————————
Singleton1 Singleton2
———————————————————————————————————————————— ————————————————————
- singleton1: Singleton1*
————————————————————————————————————————————
+ getInstance(): static SingletonDerived1* --- if (! singleton1)
| singleton1 = new Singleton1();
| return singletonDerived1;
- Singleton1()
+ destoryInstance(): static void --- if (Singleton1::singleton1)
| delete Singleton1::singleton1;
|/ Singleton1::singleton1 = nullptr;
+ ~Singleton1()
+ f(): void
————————————————————————————————————————————
——————————————————————————————————————————————————————
Base1
——————————————————————————————————————————————————————
——————————————————————————————————————————————————————
+ getInstance(const char* name): static SingletonBase* ---
| if (strcmp(name, "SingletonDerived1") == 0)
| return SingletonDerived1::getInstance();
# singletonBase() else if (strcmp(name, "SingletonDerived2") == 0)
return SingletonDerived2::getInstance();
else
return nullptr;
+ destoryInstance(): virtual void
+ vf(): virtual void
——————————————————————————————————————————————————————
/\ /\
/__\ /__\
| |
| |
| ——————————————————————
| Base2
| ——————————————————————
|
| ——————————————————————
| + vf2(): virtual void
| ——————————————————————
| /\
| /__\
| |
| |
|
———————————————————————————————————————————— ———————————————————
SingletonDerived1 SingletonDerived2
———————————————————————————————————————————— ————————————————————
- singletonDerived1: SingletonDerived1*
————————————————————————————————————————————
+ getInstance(): static SingletonDerived1* --- if (! singletonDerived1)
| singletonDerived1 = new SingletonDerived1();
| return singletonDerived1;
- SingletonDerived1()
+ destoryInstance(): void delete singletonDerived1;
| singletonDerived1 = nullptr;
|
|/
+ ~SingletonDerived1()
+ vf(): virtual void
————————————————————————————————————————————
————————————————————————————————————————————
CManager
————————————————————————————————————————————
_ _ /\ - cLst: std::list<C> 管理多张 Board1卡(共享1个 单例子类1对象)
| \/ ————————————————————————————————————————————
|
| ——————|——————————————————————————————————————
| |
| 统一销毁 各子类单例
| SingletonBase::getInstance("SingletonDerived1")->destoryInstance();
| SingletonBase::getInstance("SingletonDerived2")->destoryInstance();
|
|
| ————————————————————————————————————————————
|- - - C
————————————————————————————————————————————
_ _ /\ - board: Board*
| \/ ————————————————————————————————————————————
|
|
| ————————————————————————————————————————————
|_ _ _ Board:接口类
————————————————————————————————————————————
/\
/__\
| |
| }
/ \
———————————————————————————————————————————— ———————————————————
Board1 Board2
———————————————————————————————————————————— ———————————————————
- singletonBase: SingletonBase*
————————————————————————————————————————————
+Rtc5Board()
—————|———————————————————————————————————————
|
singletonBase = SingletonBase::getInstance("SingletonDerived1")
// Base.h
#ifndef SINGLETONBASE_H
#define SINGLETONBASE_H
class Base
{
public:
// (1) Note1: Base::getInstance()-> Derived::getInstance() -> private Ctor: SingletonDerived1() -> protected Base Ctor: Base() {}
static Base* getInstance(const char* name);
virtual void destoryInstance() = 0;
virtual void vf() = 0;
};
#endif
// Base2.h
#ifndef SINGLETONBASE2_H
#define SINGLETONBASE2_H
#include "Base.h"
class Base2: public Base
{
public:
virtual void vf2() = 0;
};
#endif
// SingletonDerived1.h
#ifndef SINGLETONDERIVED1_H
#define SINGLETONDERIVED1_H
#include "Base.h"
class SingletonDerived1 : public Base
{
public:
static SingletonDerived1* getInstance();
// Note4: destoryInstance() -> Derived::virtial Dtor -> Base:: virtual Dtor
virtual void destoryInstance();
virtual ~SingletonDerived1();
virtual void vf();
private:
static SingletonDerived1* singletonDerived1;
private:
SingletonDerived1() { }
};
#endif
// SingletonDerived2.h
// #pragma
#ifndef SINGLETONDERIVED2_H
#define SINGLETONDERIVED2_H
#include "Base2.h"
class SingletonDerived2 : public Base2
{
public:
static SingletonDerived2* getInstance();
virtual void destoryInstance();
virtual ~SingletonDerived2();
virtual void vf();
virtual void vf2();
private:
static SingletonDerived2* singletonDerived2;
SingletonDerived2() { }
};
#endif
// SingletonDerived1.cpp
#include "SingletonDerived1.h"
#include <iostream>
SingletonDerived1* SingletonDerived1::singletonDerived1 = nullptr;
SingletonDerived1* SingletonDerived1::getInstance()
{
if (!singletonDerived1)
singletonDerived1 = new SingletonDerived1();
return singletonDerived1;
}
void SingletonDerived1::destoryInstance()
{
if (singletonDerived1)
{
delete singletonDerived1;
singletonDerived1 = nullptr;
}
}
SingletonDerived1::~SingletonDerived1()
{
}
void SingletonDerived1::vf()
{
std::cout << "SingletonDerived1::vf()\n";
}
// SingletonDerived2.cpp
#include "SingletonDerived2.h"
#include <iostream>
SingletonDerived2* SingletonDerived2::singletonDerived2 = nullptr;
SingletonDerived2* SingletonDerived2::getInstance()
{
if (!singletonDerived2)
singletonDerived2 = new SingletonDerived2();
return singletonDerived2;
}
void SingletonDerived2::destoryInstance()
{
if (singletonDerived2)
{
delete singletonDerived2;
singletonDerived2 = nullptr;
}
}
SingletonDerived2::~SingletonDerived2()
{
}
void SingletonDerived2::vf()
{
std::cout << "SingletonDerived2::vf()\n";
}
void SingletonDerived2::vf2()
{
std::cout << "SingletonDerived2::vf2()\n";
}
// SingletonBaseFactory.cpp
#include "SingletonDerived1.h" // already #include "Base.h"
#include "SingletonDerived2.h"
#include <cstring>
Base* Base::getInstance(const char* name)
{
if (strcmp(name, "SingletonDerived1") == 0)
return SingletonDerived1::getInstance();
else if (strcmp(name, "SingletonDerived2") == 0)
return SingletonDerived2::getInstance();
else
return nullptr;
}
// main.cpp
#include "Base.h"
#include "Base2.h"
int main()
{
Base::getInstance("SingletonDerived1")->vf();
Base::getInstance("SingletonDerived2")->vf();
if (auto p = dynamic_cast<Base2*>(Base::getInstance("SingletonDerived2") ) )
p->vf2();
Base::getInstance("SingletonDerived1")->vf();
// (5) Note5: 统一销毁各子类单例
Base::getInstance("SingletonDerived1")->destoryInstance();
Base::getInstance("SingletonDerived2")->destoryInstance();
}