多模式的 C++ 模板化类实现

问题描述 投票:0回答:4

我使用 C++ 中的模板化类实现了多模式。

#ifndef MULTITON_H
#define MULTITON_H

#include <map>

template <typename Key, typename T> class Multiton
{
public:    
    static void destroy()
    {
        for (typename std::map<Key, T*>::iterator it = instances.begin(); it != instances.end(); ++it) {
            delete (*it).second;
        }
    }

    static T& getRef(const Key& key)
    {
        typename std::map<Key, T*>::iterator it = instances.find(key);

        if (it != instances.end()) {
            return *(T*)(it->second);
        }

        T* instance = new T;
        instances[key] = instance;
        return *instance;
    }

    static T* getPtr(const Key& key)
    {
        typename std::map<Key, T*>::iterator it = instances.find(key);

        if (it != instances.end()) {
            return (T*)(it->second);
        }

        T* instance = new T;
        instances[key] = instance;
        return instance;
    }

protected:
    Multiton() {}
    virtual ~Multiton() {}

private:
    Multiton(const Multiton&) {}
    Multiton& operator= (const Multiton&) { return *this; }

    static std::map<Key, T*> instances;
};

template <typename Key, typename T> std::map<Key, T*> Multiton<Key, T>::instances;

#endif

用法:

class Foo : public Multiton<std::string, Foo> {};
Foo& foo1 = Foo::getRef("foobar");
Foo* foo2 = Foo::getPtr("foobar");
Foo::destroy();

有什么改进建议吗?

c++ singleton design-patterns multiton
4个回答
3
投票

1) 个人偏好,但我会颠倒模板参数的顺序并将 Key 默认为 std::string (如果这是你最常使用的)

template <typename Key, typename T> class Multiton { ... }

然后你可以这样做:

class Foo : public Multiton<Foo> {};
class Bar : public Multiton<Bar,int> {};

我觉得哪个更好。

2)此外,如果您从不将指针/引用传递给 Multitron(这不会违反模式),则您不需要在类中使用虚拟析构函数。

3) 如果你为你的 T*s 使用了一个更智能的容器,你可以避免调用 Foo::destroy()。 像

std::map<Key,boost::shared_ptr<T> >
这样的东西会在静态实例被销毁时销毁所有对象。 (尽管如果你关心破坏顺序,那么你需要一些更聪明的东西——你可以从现有的单例解决方案中改编一些东西,比如凤凰单例等)

4) 您可以将迭代器更改为 const_iterators。

5) destroy 应该清除映射以防止在调用 destroy 后意外访问无效内存。或者,如果你想防止这种情况发生,你应该抛出一个异常。

Foo* foo2 = Foo::getPtr("foobar");
Foo::destroy();
Foo::getPtr("foobar")->doSomething(); // BANG

6) 如果你不使用多态 T 那么你可以使用 std::map 并且你的代码看起来像这样......

template <typename Key, typename T> class Multiton
{
public:
    //Can probably get rid of this guy as maps destructor will do the right thing 
    static void destroy()
    {
        instances.clear();
    }

    static T& getRef(const Key& key)
    {
        return instances[key];
    }

    static T* getPtr(const Key& key)
    {
        return &instances[key];
    }

protected:
    Multiton() {}
    virtual ~Multiton() {}

private:
    Multiton(const Multiton&) {}
    Multiton& operator= (const Multiton&) { return *this; }

    static std::map<Key, T> instances;
};

暂时能想到的就这些了


2
投票

一个改进是重写

getRef
以使用
getPtr
(反之亦然,方向并不重要不重复自己):

static T& getRef(const Key& key)
{
    return *getPtr(key);
}

0
投票

看来你做得非常出色。

顺便说一句,请问你为什么在这里铸造实例(c 风格)?

return (T*)(it->second);

我认为如果你只写它会更干净

return it->second;

此外,由于这篇文章已有 10 年历史, 我使用智能指针以现代 C++ 方式对版本进行了一些升级。 请看一看!

multiton.h

#ifndef MULTITON_H
#define MULTITON_H

#include <map>
#include <string>

template <typename T, typename Key = int> class Multiton {
  public:
    static void DestroyAll();
    static void Destroy(const Key &key);
    static std::shared_ptr<T> GetPtr(const Key &key);
    static T &GetRef(const Key &key) { return *GetPtr(key); }

  protected:
    Multiton();
    ~Multiton();

  private:
    Multiton(const Multiton &) = default;
    Multiton &operator=(const Multiton &) = default;
    static std::map<Key, std::shared_ptr<T>> instances_;
};

#endif // MULTITON_H

multiton.cpp

#include "multiton.h"
template <typename T, typename Key> void Multiton<T, Key>::DestroyAll() {
    for (auto it = instances_.begin(); it != instances_.end(); ++it)
        delete (*it).second;
    instances_.clear();
}

template <typename T, typename Key> void Multiton<T, Key>::Destroy(const Key &key) {
    auto it = instances_.find(key);

    if (it != instances_.end()) {
        delete (*it).second;
        instances_.erase(it);
    }
}

template <typename T, typename Key> std::shared_ptr<T> Multiton<T, Key>::GetPtr(const Key &key) {
    const auto it = instances_.find(key);

    if (it != instances_.end())
        return (it->second);

    std::shared_ptr<T> instance = std::make_shared<T>();
    instances_[key] = instance;
    return instance;
}

template <typename T, typename Key> Multiton<T, Key>::Multiton() {}

template <typename T, typename Key> Multiton<T, Key>::~Multiton() {}

template <typename T, typename Key> std::map<Key, std::shared_ptr<T>> Multiton<T, Key>::instances_;

-1
投票

这里是多种设计模式的有用链接: https://codedesignhub.com/design-patterns/multiton-design-pattern/

© www.soinside.com 2019 - 2024. All rights reserved.