|  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
54
55
56
57
58
 | template<typename T>
class Singleton
{
public:
    static std::shared_ptr<T> getInstance()
    {
        if (m_instance == nullptr)
        {
            m_instance = std::make_shared<T>();
            atexit(destroy);
        }
        return m_instance;
    }
protected:
    Singleton()
    {
    }
    virtual ~Singleton()
    {
    }
    Singleton(const Singleton&)
    {
    }
private:
    static void destroy()
    {
        delete m_instance;
    }
    static std::shared_ptr<T> m_instance;
};
template <typename T> std::shared_ptr<T> Singleton <T>::m_instance;
class TemplateFactory :
    public Singleton<TemplateFactory>
{
public:
    template <typename T>
    std::shared_ptr<Flyweight> getCode(int key)
    {
        if (m_list.find(key) == m_list.end())
        {
            m_list[key] = std::make_shared<T>();
        }
    }
private:
    std::map<int, std::shared_ptr<Flyweight>> m_list;
};
 |