类和应用对象管理统一改为shared_ptr
parent
e3c2ea210e
commit
92a17eed14
|
@ -86,17 +86,17 @@ public:
|
|||
class Factory
|
||||
{
|
||||
public:
|
||||
AbstractSportProduct *getSportProduct(string productName)
|
||||
std::shared_ptr<AbstractSportProduct> getSportProduct(string productName)
|
||||
{
|
||||
AbstractSportProduct *pro = NULL;
|
||||
std::shared_ptr<AbstractSportProduct> pro;
|
||||
if (productName == "Basketball"){
|
||||
pro = new Basketball();
|
||||
pro = std::shared_ptr<AbstractSportProduct>(new Basketball());
|
||||
}
|
||||
else if (productName == "Football"){
|
||||
pro = new Football();
|
||||
pro = std::shared_ptr<AbstractSportProduct>(new Football());
|
||||
}
|
||||
else if (productName == "Volleyball"){
|
||||
pro = new Volleyball();
|
||||
pro = std::shared_ptr<AbstractSportProduct>(new Volleyball());
|
||||
}
|
||||
return pro;
|
||||
}
|
||||
|
|
|
@ -8,13 +8,16 @@ int main()
|
|||
|
||||
//定义工厂类对象
|
||||
std::shared_ptr<Factory> fac = std::make_shared<Factory>();
|
||||
std::shared_ptr<AbstractSportProduct>product = std::shared_ptr<AbstractSportProduct>(fac->getSportProduct("Basketball"));
|
||||
// std::shared_ptr<AbstractSportProduct> product = std::shared_ptr<AbstractSportProduct>(fac->getSportProduct("Basketball"));
|
||||
std::shared_ptr<AbstractSportProduct> product = fac->getSportProduct("Basketball");
|
||||
|
||||
fac = std::make_shared<Factory>();
|
||||
product = std::shared_ptr<AbstractSportProduct>(fac->getSportProduct("Football"));
|
||||
product = fac->getSportProduct("Football");
|
||||
// product = std::shared_ptr<AbstractSportProduct>(fac->getSportProduct("Football"));
|
||||
|
||||
fac = std::make_shared<Factory>();
|
||||
product = std::shared_ptr<AbstractSportProduct>(fac->getSportProduct("Volleyball"));
|
||||
product = fac->getSportProduct("Volleyball");
|
||||
// product = std::shared_ptr<AbstractSportProduct>(fac->getSportProduct("Volleyball"));
|
||||
#ifdef win32
|
||||
system("pause");
|
||||
#endif
|
||||
|
|
Loading…
Reference in New Issue