gpt4 book ai didi

c++ - 工厂模式和 std::bind 方法

转载 作者:行者123 更新时间:2023-11-27 23:41:52 28 4
gpt4 key购买 nike

几周前,我使用以下指南在 C++ 中实现了一个工厂模式:https://www.codeproject.com/Articles/363338/Factory-Pattern-in-Cplusplus

从那时起它运行良好:工厂模式中使用的所有方法都返回相同的类型,并且也采用相同的参数类型,直到今天。我现在需要为我使用的方法添加绑定(bind)新参数,因此方法签名不再相同。

我目前正在使用 2 个东西,并实现第三个:

1) Action :每个都表示为一个类并包含一个静态 AAction::ptr create(void* buff) 方法。 Actions 继承自 AAction 类。 Action 可以使用它们自己的 serialize() 内部方法序列化,或者使用它们的 create(buff) 静态方法反序列化。缓冲区参数包含调用 LoginAction() 构造函数所需的 ID 和密码。

class AAction {
typedef std::unique_ptr<AAction> ptr;
};

class LoginAction : public AAction {
private:
std::string id;
std::string password;
bool authenticated;

public:
LoginAction(std::string id, std::string password);
virtual ~LoginAction() = default;

void execute();

static AAction::ptr create(const void* buffer);
};

2) ActionFactory:用于通过从正确的类调用适当的 create() 静态方法来反序列化传入的操作。

class ActionFactory {
typedef std::unique_ptr<AAction> (*CreateActionFn)(const void*);
typedef std::map<RawActionType, CreateActionFn> RawFactoryMap;

RawFactoryMap rawFactoryMap;
public:
ActionFactory(Authenticator& authenticator) {
this->registerMethod(RawActionType_RawLoginAction, &LoginAction::create);
this->registerMethod(RawActionType_RawLogoutAction, &LogoutAction::create);
this->registerMethod(RawActionType_RawStandAction, &StandAction::create);
}

void registerMethod(const RawActionType &rawActionType, CreateActionFn pfnCreate);

std::unique_ptr<AAction> getAction(RawActionType rawActionType, const void* buffer);
};

只需调用 execute() 方法即可在代码中随时执行操作,无需任何参数。

到目前为止,一切正常。问题是我现在需要为未存储在密码中的操作添加更多参数。例如在我的例子中:一个 Authenticator

3) 一个 Authenticator,用于验证用户。

所以在 LoginAction::execute() 中,我所要做的就是调用

this->authenticator.authenticate(this->id, this->password).

以下是我为此所做的更改:

  • 我向 LoginAction 构造函数添加了验证器:LoginAction(Authenticator& authenticator, std::string id, std::string password);

    还有一个字段:Authenticator&认证器;

  • 我将身份验证器添加到 LoginAction::create 静态方法:

    static AAction::ptr create(const void* buffer, Authenticator& authenticator);

  • 我在 ActionFactory 构造函数中修改了注册方法的方式,使用 std::bind :

    this->registerMethod(RawActions_RawLoginAction, std::bind(&LoginAction::create, std::placeholders::_1, authenticator);

但是,由于我的函数类型发生了变化,我无法再将其存储在 RawFactoryMap 中。

error: invalid cast from type ‘std::_Bind_helper ()(const void, Authenticator&), const std::_Placeholder<1>&, Authenticator&>::type {aka std::_Bind ((std::_Placeholder<1>, Authenticator))(const void, Authenticator&)>}’ to type ‘ActionFactory::CreateActionFn {aka std::unique_ptr ()(const void)}’

在 ActionFactory 中保留函数映射并遵守工厂模式的最佳方法是什么?

提前致谢,祝您有美好的一天!

作为附加说明:我很开放,很乐意阅读任何关于如何改进我的代码的建议,即使是一些小的改进。

最佳答案

要事第一。对于 C++11,强烈推荐 usingtypedef .考虑以下之间的可读性差异:

typedef std::unique_ptr<AAction> (*CreateActionFn)(const void*);
typedef std::map<RawActionType, CreateActionFn> RawFactoryMap;

和:

using CreateActionFn = std::unique_ptr<AAction>(*)(const void*);
using RawFactoryMap = std::map<RawActionType, CreateActionFn>;

当名称出现在左侧而不是任意位置时,这很好。


也就是说,既然函数指针不够用,因为您需要存储状态,您需要泛化为任意可调用对象。这就是 std::function 用于:提供的签名的类型删除可调用对象:

using CreateActionFn = std::function<std::unique_ptr<AAction>(const void*)>;

这将匹配任何可复制的可调用对象,您可以使用 const void* 调用它得到 unique_ptr<AAction> .


当我们在这里时,不要使用 std::bind :

 std::bind(&LoginAction::create, std::placeholders::_1, authenticator)

使用 lambda:

[authenticator](void const* ptr){ return LoginAction::create(ptr, authenticator); }

或:

[=](void const* ptr){ return LoginAction::create(ptr, authenticator); }

它可能不会更短,但更容易阅读。

关于c++ - 工厂模式和 std::bind 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54092804/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com