blob: dd496fa6fc396b27cbe9468eaa61b8609b9197f9 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
#ifndef REGISTER_IMPLEMENT_H_
#define REGISTER_IMPLEMENT_H_
#include <map>
namespace meow{
template<class T>
class ImplementInterface{
private:
T identify_;
protected:
ImplementInterface(T const& id): identify_(id) { }
public:
T const& identify() const { return identify_; }
virtual ~ImplementInterface(){ }
};
//
template<class T>
class RegisterInterface{
private:
std::map<T, ImplementInterface<T>*> implements;
protected:
RegisterInterface();
public:
virtual bool regImplement(ImplementInterface<T>*imp);
virtual ImplementInterface<T>* getImplement(T const& identify);
virtual ~RegisterInterface(){ }
};
/*******************************************************************
@asciidoc
=== meow:: *ImplementInterface/RegisterInterface* (C++ Class)
.Description
Assume there is a problem which can be solved by different algorithms.
Then you can write multiple classes to approach this problem. +
Now if you want to decide which algorithm to use in runtime, you can just
approach this case by a simple way:
* Let all the problem-solving classes inherit from
`class ImplementInterface<T>` , and call the constructure with giving
`identify` (type `T` ) .
* Create an object, type `RegisterInterface<T>` , and register all your
implement class to it by call `regImplement(pointer to the class)`.
* Select which implement class you want by call `getImplement(identify)` ,
which will return the pointer to the corresponding class.
'''
@asciidoc-
******************************************************************/
}
#include "Register_Implement.hpp"
#endif // REGISTER_IMPLEMENT_H_
|