c ++我可以使用std :: unique_ptr与依赖注入吗? [英] c++ Can I use std::unique_ptr with dependency injection?
问题描述
我一直在使用原始指针进行依赖注入,我决定将我的代码转换为使用shared_ptr。这工作,但我想知道如果我可以使用unique_ptr而不是?在下面的例子中,MyClass将管理信用卡服务的生命周期。
class PaymentProcessor
{
PaymentProcessor(?? creditCardService):
:creditCardService_ )
{
}
private:
CreditCardService * creditCardService_;
}
class MyClass
{
public:
void DoIt()
{
creditCardService_.reset(new VisaCardService ));
PaymentProcessor pp(creditCardService_);
pp.ProcessPayment();
}
private:
std :: unique_ptr< CreditCardService> creditCardService_;
}
你可以传递一个unique_ptr到另一个类, 指针(没有拥有它?)?如果是这样,这是一个好主意,参数的类型应该在PaymentProcessor的构造函数中?
UPDATE
在上面的例子中,我可以在堆栈上创建一个 VisaCardService
变量,并且 PaymentProcessor
构造函数将此作为参考参数。这似乎是推荐的C ++实践。然而,在具体类型的creditCardService_在运行时间之前是未知的(例如,用户选择特定的信用卡服务在运行时使用),使用 std :: unique_ptr $ c $
您可以将unique_ptr传递给另一个类,其中其他类是
只是使用指针(不拥有它?)
指向参考的指针:
class PaymentProcessor
{
public:
PaymentProcessor(CreditCardService& creditCardService_):
:creditCardService_(creditCardService_)
{
}
private:
CreditCardService& creditCardService_;
};
void DoIt()
{
creditCardService_.reset(new VisaCardService());
PaymentProcessor pp(* creditCardService_);
pp.ProcessPayment();
}
如果您仍然想使用指针,则需要使用 get
方法:
class PaymentProcessor
{
public :
PaymentProcessor(CreditCardService * creditCardService_):
:creditCardService_(creditCardService_)
{
}
private:
CreditCardService * creditCardService_;
};
void DoIt()
{
creditCardService_.reset(new VisaCardService());
PaymentProcessor pp(creditCardService_.get());
pp.ProcessPayment();
}
I had been doing dependency injection using raw pointers and I decided to convert my code to use shared_ptr. This works but I'm wondering if I could use unique_ptr instead? In my example below, MyClass would manage the lifetime of the credit card service.
class PaymentProcessor
{
PaymentProcessor(?? creditCardService):
:creditCardService_(creditCardService)
{
}
private:
CreditCardService *creditCardService_;
}
class MyClass
{
public:
void DoIt()
{
creditCardService_.reset(new VisaCardService());
PaymentProcessor pp(creditCardService_);
pp.ProcessPayment();
}
private:
std::unique_ptr<CreditCardService> creditCardService_;
}
Can you pass a unique_ptr to another class where the other class is just "using" the pointer (without owning it??)? If so is this a good idea and what should the type of the parameter be in the constructor for PaymentProcessor?
UPDATE
In the example as shown above I can alternatively create a VisaCardService
variable on the stack and have the PaymentProcessor
constructor take this as a reference parameter. This seems to be the recommended C++ practice. However, in the case where the concrete type of creditCardService_ is not known until runtime (e.g., the user chooses the particular Credit Card Service to use at runtime), is using std::unique_ptr
with references the best solution?
Can you pass a unique_ptr to another class where the other class is just "using" the pointer (without owning it??)?
In that case, change the pointer to reference :
class PaymentProcessor
{
public:
PaymentProcessor(CreditCardService & creditCardService_):
:creditCardService_(creditCardService_)
{
}
private:
CreditCardService &creditCardService_;
};
void DoIt()
{
creditCardService_.reset(new VisaCardService());
PaymentProcessor pp(*creditCardService_);
pp.ProcessPayment();
}
If you still want to use a pointer, then you need to use get
method :
class PaymentProcessor
{
public:
PaymentProcessor(CreditCardService * creditCardService_):
:creditCardService_(creditCardService_)
{
}
private:
CreditCardService *creditCardService_;
};
void DoIt()
{
creditCardService_.reset(new VisaCardService());
PaymentProcessor pp(creditCardService_.get());
pp.ProcessPayment();
}
这篇关于c ++我可以使用std :: unique_ptr与依赖注入吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!