简体   繁体   English

将智能指针与工厂模式一起使用

[英]Using Smart Pointers with Factory Pattern

I am trying to use smart pointers in factory design pattern. 我正在尝试在工厂设计模式中使用智能指针。 I made a google search to get some ideas but could not find any kind of implementation, but lots of great ideas. 我进行了一次Google搜索,以获取一些想法,但是找不到任何实现方法,但是有很多很棒的想法。 So, I wrote my own code, but there are two points I am not sure. 因此,我编写了自己的代码,但是我不确定两点。

First, I will add my code and then ask my questions. 首先,我将添加代码,然后提出问题。

// AgentGameStyleFactory.hpp

#ifndef AgentGameStyleFactory_hpp
#define AgentGameStyleFactory_hpp

#include "AgentGameStyle.hpp"

#include <memory>

class AgentGameStyleFactory
{
public:

    static std::unique_ptr<AgentGameStyle> instantiate(const AgentPlayingStyleData::ePLAYING_CHARACTER pStyle);

private:

    static AgentGameStyle* instantiateHelper(const AgentPlayingStyleData::ePLAYING_CHARACTER pStyle);
};

#endif /* AgentGameStyleFactory_hpp */
// AgentGameStyleFactory.cpp
#include "AgentGameStyleFactory.hpp"

using namespace AgentPlayingStyleData;

std::unique_ptr<AgentGameStyle> AgentGameStyleFactory::instantiate(const ePLAYING_CHARACTER pStyle)
{
    std::unique_ptr<AgentGameStyle> newStyle( instantiateHelper(pStyle) );
    return std::move(newStyle);
}

AgentGameStyle* AgentGameStyleFactory::instantiateHelper(const ePLAYING_CHARACTER pStyle)
{
    AgentGameStyle* newStyle = NULL;

    switch(pStyle)
    {
        case ePLAYING_CHARACTER::ePC_CONTAIN:
            newStyle = new ContainGameStyleAgent();
            break;
        case ePLAYING_CHARACTER::ePC_COUNTER:
            newStyle = new CounterGameStyleAgent();
            break;
        case ePLAYING_CHARACTER::ePC_STANDARD:
            newStyle = new StandardGameStyleAgent();
            break;
        case ePLAYING_CHARACTER::ePC_ATTACKING:
            newStyle = new AttackGameStyleAgent();
        case ePLAYING_CHARACTER::ePC_OVERLOAD:
            newStyle = new OverloadGameStyleAgent();
            break;
        default:
            newStyle = new StandardGameStyleAgent();
            break;
    }

        return newStyle;
}

As you can see, I am creating the related style in the factory and then returning and assigning it as 如您所见,我正在工厂中创建相关样式,然后将其返回并分配为

mPlayingCharacteristic = AgentGameStyleFactory::instantiate(pPlayingCharacteristic);

where mPlayingCharacteristic is std::unique_ptr<AgentGameStyle> 其中mPlayingCharacteristicstd::unique_ptr<AgentGameStyle>

My first questions is about returning the unique_ptr . 我的第一个问题是关于返回unique_ptr According to the posts I have read, it seems correct, but the compiler (Xcode) gives me "Moving a local object in a return statement prevents copy elision" warning. 根据我读过的帖子,这似乎是正确的,但是编译器(Xcode)给我"Moving a local object in a return statement prevents copy elision"警告。 Is this something normal, something I should expect to have or is something wrong here? 这是正常现象,我应该期待的事物还是这里有问题?

My second question is whether I am initiating std::unique_ptr<AgentGameStyle> newStyle in AgentGameStyleFactory.cpp correctly, which means using a helper method as AgentGameStyleFactory::instantiateHelper is right thing to do? 我的第二个问题是我是否正确std::unique_ptr<AgentGameStyle> newStyleAgentGameStyleFactory.cpp std::unique_ptr<AgentGameStyle> newStyle ,这意味着将辅助方法用作AgentGameStyleFactory::instantiateHelper是正确的做法吗?

Your compiler is right to warn you about return std::move(...) - you can (and should) simply write: 您的编译器可以警告您有关return std::move(...) -您可以(并且应该)简单地编写:

return newStyle;

I'd encourage you to ditch the separate helper with its bare new , and just create the smart pointer directly (assuming C++14 or later): 我鼓励您放弃带有单独的new的单独的帮助程序,而直接创建智能指针(假设使用C ++ 14或更高版本):

std::unique_ptr<AgentGameStyle> AgentGameStyleFactory::instantiate(const ePLAYING_CHARACTER pStyle)
{
   switch(pStyle) {
        case ePLAYING_CHARACTER::ePC_CONTAIN:
            return std::make_unique<ContainGameStyleAgent>();
        case ePLAYING_CHARACTER::ePC_COUNTER:
            return std::make_unique<CounterGameStyleAgent>();
        case ePLAYING_CHARACTER::ePC_STANDARD:
            return std::make_unique<StandardGameStyleAgent>();
        case ePLAYING_CHARACTER::ePC_ATTACKING:
            return std::make_unique<AttackGameStyleAgent>();
        case ePLAYING_CHARACTER::ePC_OVERLOAD:
            return std::make_unique<OverloadGameStyleAgent>();
        default:
            return std::make_unique<StandardGameStyleAgent>();
    }
}

There is no need for "helper method" returning raw pointers and there is no need to call new manually: 不需要“ helper方法”返回原始指针,也不需要手动调用new

std::unique_ptr<AgentGameStyle>
AgentGameStyleFactory::instantiate(const ePLAYING_CHARACTER pStyle)
{
    switch(pStyle)
    {
        case ePLAYING_CHARACTER::ePC_CONTAIN:
        {
            return std::make_unique<ContainGameStyleAgent>();
        }
        //...
        default:
        {
            return std::make_unique<StandardGameStyleAgent>();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM