工厂方法创建的对象应该在哪里删除?
Where should the objects created by a factory method be deleted?
returnShapeType
创建的对象应该如何删除以及在哪里删除?
这是一个工厂方法演示程序。
请出示代码。
class Shape
{
public:
Shape() {}
virtual void print() {std::cout << "\nFrom shape print";}
};
class Triangle: public Shape
{
public:
Triangle(){}
virtual void print() {std::cout << "\nFrom triangle print";}
};
class Rectangle: public Shape
{
public:
Rectangle(){}
virtual void print() {std::cout << "\nFrom rect print";}
};
class CreateShapeObject
{
public:
CreateShapeObject() {}
Shape *returnShapeType( std::string arg )
{
if (arg == "Triangle")
return new Triangle;
else if (arg == "Rectangle")
return new Rectangle;
}
};
////////////
class EndDeveloper
{
public:
CreateShapeObject obj;
EndDeveloper()
{
Shape *p = obj.returnShapeType("Triangle");
p->print();
Shape *q = obj.returnShapeType("Rectangle");
q->print();
}
};
像对待 new
.
的任何其他用途一样对待工厂对 new
的任何使用
IE。使用 new
(是否通过工厂)的 code/class 也负责执行 delete
。
您必须在设计中建立所有权原则。
在您发布的代码中,CreateShapeObject
不保留指向所构造对象的指针。它只是 returns 指向构造对象的指针。这意味着调用 function/class 必须取得对象的所有权。他们应该负责删除它,除非他们将所有权转让给另一个 function/class,在这种情况下,另一个 function/class 应该负责删除它。
如果您想让 CreateShapeObject
负责删除它构造的对象,则必须更新它以跟踪它构造的对象。那时,您可能想要更改 class 的名称以反映双重责任。类似于 ShapeObjectManager
的内容会更有意义。
使用原始指针很容易出错。使用 unique_ptr
:
std::unique_ptr<Shape> returnShapeType(const std::string& arg)
{
if (arg == "Triangle")
return std::make_unique<Triangle>();
else if (arg == "Rectangle")
return std::make_unique<Rectangle>();
throw std::invalid_argument("Invalid shape");
}
配合auto
可以方便的使用:
auto shape = obj.returnShapeType("Triangle");
unique_ptr
也可以隐式转换为shared_ptr
:
std::shared_ptr<Shape> shape = obj.returnShapeType("Triangle");
returnShapeType
创建的对象应该如何删除以及在哪里删除?
这是一个工厂方法演示程序。
请出示代码。
class Shape
{
public:
Shape() {}
virtual void print() {std::cout << "\nFrom shape print";}
};
class Triangle: public Shape
{
public:
Triangle(){}
virtual void print() {std::cout << "\nFrom triangle print";}
};
class Rectangle: public Shape
{
public:
Rectangle(){}
virtual void print() {std::cout << "\nFrom rect print";}
};
class CreateShapeObject
{
public:
CreateShapeObject() {}
Shape *returnShapeType( std::string arg )
{
if (arg == "Triangle")
return new Triangle;
else if (arg == "Rectangle")
return new Rectangle;
}
};
////////////
class EndDeveloper
{
public:
CreateShapeObject obj;
EndDeveloper()
{
Shape *p = obj.returnShapeType("Triangle");
p->print();
Shape *q = obj.returnShapeType("Rectangle");
q->print();
}
};
像对待 new
.
的任何其他用途一样对待工厂对 new
的任何使用
IE。使用 new
(是否通过工厂)的 code/class 也负责执行 delete
。
您必须在设计中建立所有权原则。
在您发布的代码中,CreateShapeObject
不保留指向所构造对象的指针。它只是 returns 指向构造对象的指针。这意味着调用 function/class 必须取得对象的所有权。他们应该负责删除它,除非他们将所有权转让给另一个 function/class,在这种情况下,另一个 function/class 应该负责删除它。
如果您想让 CreateShapeObject
负责删除它构造的对象,则必须更新它以跟踪它构造的对象。那时,您可能想要更改 class 的名称以反映双重责任。类似于 ShapeObjectManager
的内容会更有意义。
使用原始指针很容易出错。使用 unique_ptr
:
std::unique_ptr<Shape> returnShapeType(const std::string& arg)
{
if (arg == "Triangle")
return std::make_unique<Triangle>();
else if (arg == "Rectangle")
return std::make_unique<Rectangle>();
throw std::invalid_argument("Invalid shape");
}
配合auto
可以方便的使用:
auto shape = obj.returnShapeType("Triangle");
unique_ptr
也可以隐式转换为shared_ptr
:
std::shared_ptr<Shape> shape = obj.returnShapeType("Triangle");