这段代码是工厂方法还是抽象工厂模式?
Is this code a factory method or abstract factory pattern?
来自What are the differences between Abstract Factory and Factory design patterns?:
Factory
Imagine you are constructing a house and you approach a carpenter for
a door. You give the measurement for the door and your requirements,
and he will construct a door for you. In this case, the carpenter is a
factory of doors. Your specifications are inputs for the factory, and
the door is the output or product from the factory.
Abstract Factory
Now, consider the same example of the door. You can go to a carpenter,
or you can go to a plastic door shop or a PVC shop. All of them are
door factories. Based on the situation, you decide what kind of
factory you need to approach. This is like an Abstract Factory.
根据那个解释,下面的代码是工厂方法还是抽象工厂模式?
class PaintShape
{
public:
static PaintShape *createShapeObject( std::string shape );
virtual void print() { std::cout << "print PaintShape"; }
};
class PaintTriangle : public PaintShape
{
public:
PaintTriangle() {}
virtual void print() { std::cout << "\nprint PaintTriangle"; }
};
class PaintRectangle : public PaintShape
{
public:
PaintRectangle() {}
virtual void print() { std::cout << "\nprint PaintRectangle"; }
};
PaintShape* PaintShape::createShapeObject( std::string shape )
{
if( shape == "triangle" )
return new PaintTriangle;
else if( shape == "rectangle" )
return new PaintRectangle;
return new PaintShape;
};
class EndDeveloper
{
public:
EndDeveloper()
{
std::string shape;
std::cout << "\nWhat shape would you like? ";
// Get input from the terminal.
std::getline( std::cin, shape );
PaintShape *p = PaintShape::createShapeObject( shape );
p->print();
std::cout << "\nWhat shape would you like? ";
std::getline (std::cin, shape);
PaintShape *s = PaintShape::createShapeObject( shape );
s->print();
}
};
如何将其转换为工厂方法and/or抽象工厂方法?
工厂方法只产生一个项目,而抽象工厂产生一些相关项目的子集。
在你的例子中,你有一个模式工厂方法。
换句话说,关于如何记住某些内容的建议并不适用于每个人。对我来说,从报价中不清楚有什么区别:)
我建议您阅读以下书籍以将事物归位:
According to that explanation, is the following code a factory method or an abstract factory pattern?
该代码不是任一模式的实现:
- 对象创建不会延迟到子classes,所以它不是工厂方法的实现。
- PaintShape 没有一组工厂方法来加强关系,因此它不是抽象工厂。
What would be the way to convert this into a factory method?
假设我们必须使用工厂方法,我们必须引入一个class负责创建形状。更令人困惑的是,这些将属于 ShapeFactory 类型,但不要将其视为抽象工厂模式的实现。不是。1
在这里,我们的客户端通过调用其 createShape() 方法(工厂方法)与 ShapeFactory 进行交互。 Subclasses override 此方法和 return 正确的 Shape。这就是工厂方法模式的意义所在:将创建延迟到子classes.
#include <iostream>
class Shape {
public:
virtual std::string text() = 0;
virtual double area() = 0;
};
class Rectangle : public Shape {
public:
std::string text() override { return "rectangle"; }
double area() override { return 1.0; }
};
class Triangle : public Shape {
public:
std::string text() override { return "triangle"; }
double area() override { return 2.0; }
};
class ShapeFactory {
public:
virtual Shape* createShape() = 0;
};
class RectangleFactory : public ShapeFactory {
public:
Shape* createShape() override {
return new Rectangle();
}
};
class TriangleFactory : public ShapeFactory {
public:
Shape* createShape() override {
return new Triangle();
}
};
ShapeFactory* makeShapeFactory(std::string shape) {
if (shape == "triangle") {
return new TriangleFactory();
} else {
return new RectangleFactory();
}
};
int main() {
// Get input from the terminal.
std::string shape;
std::cout << "What shape would you like? ";
std::getline(std::cin, shape);
ShapeFactory* p = makeShapeFactory(shape);
Shape* s = p->createShape();
std::cout << s->text() << std::endl;
delete p;
delete s;
return 0;
}
I would be grateful if you could add the implementation of abstract factory method too.
ShapeFactory 现在可以创建二维或三维形状。现在我们有两个不相交的 class 层次结构:TwoDShape 和 ThreeDShape。但是,我们希望客户使用正确的 2D 和 3D subclasses。这里有一个隐含的关系,如果我们能在代码中明确说明会更好。这就是抽象工厂模式的意义所在:加强关系。
现在我们的 ShapeFactory 有两个工厂方法:create2dShape() 和 create3dShape()。当客户端与 ShapeFactory 交互时,它们会绑定到正确的 TwoDShape 和 ThreeDShape classes。例如,一个 RectangleFactory 工厂创建 TwoDRectangles 和 ThreeDRectangles。
#include <iostream>
class TwoDShape {
public:
virtual std::string text() = 0;
virtual double area() = 0;
};
class TwoDRectangle : public TwoDShape {
public:
std::string text() override { return "2d rectangle"; }
double area() override { return 1.0; }
};
class TwoDTriangle : public TwoDShape {
public:
std::string text() override { return "2d triangle"; }
double area() override { return 2.0; }
};
class ThreeDShape {
public:
virtual std::string text() = 0;
virtual double volume() = 0;
};
class ThreeDRectangle : public ThreeDShape {
public:
std::string text() override { return "3d rectangle"; }
double volume() override { return 2.0; }
};
class ThreeDTriangle : public ThreeDShape {
public:
std::string text() override { return "3d triangle"; }
double volume() override { return 4.0; }
};
class ShapeFactory {
public:
virtual TwoDShape* create2dShape() = 0;
virtual ThreeDShape* create3dShape() = 0;
};
class RectangleFactory : public ShapeFactory {
public:
TwoDShape* create2dShape() override {
return new TwoDRectangle();
}
ThreeDShape* create3dShape() override {
return new ThreeDRectangle();
}
};
class TriangleFactory : public ShapeFactory {
public:
TwoDShape* create2dShape() override {
return new TwoDTriangle();
}
ThreeDShape* create3dShape() override {
return new ThreeDTriangle();
}
};
ShapeFactory* makeShapeFactory(std::string shape) {
if (shape == "triangle") {
return new TriangleFactory();
} else {
return new RectangleFactory();
}
};
int main() {
// Get input from the terminal.
std::string shape;
std::cout << "What shape would you like? ";
std::getline(std::cin, shape);
ShapeFactory* p = makeShapeFactory(shape);
TwoDShape* s2 = p->create2dShape();
ThreeDShape* s3 = p->create3dShape();
std::cout << s2->text() << " and " << s3->text() << std::endl;
delete p;
delete s2;
delete s3;
return 0;
}
如前所述,抽象工厂是通过工厂方法实现的。这就是人们混淆它们的原因。然而,正是他们的 关注点 使他们与众不同。应用模式时,始终首先考虑他们的关注点,然后再考虑他们的实现。
1 参见 what's the advantage of using factory method pattern over simple factory?
来自What are the differences between Abstract Factory and Factory design patterns?:
Factory
Imagine you are constructing a house and you approach a carpenter for a door. You give the measurement for the door and your requirements, and he will construct a door for you. In this case, the carpenter is a factory of doors. Your specifications are inputs for the factory, and the door is the output or product from the factory.
Abstract Factory
Now, consider the same example of the door. You can go to a carpenter, or you can go to a plastic door shop or a PVC shop. All of them are door factories. Based on the situation, you decide what kind of factory you need to approach. This is like an Abstract Factory.
根据那个解释,下面的代码是工厂方法还是抽象工厂模式?
class PaintShape
{
public:
static PaintShape *createShapeObject( std::string shape );
virtual void print() { std::cout << "print PaintShape"; }
};
class PaintTriangle : public PaintShape
{
public:
PaintTriangle() {}
virtual void print() { std::cout << "\nprint PaintTriangle"; }
};
class PaintRectangle : public PaintShape
{
public:
PaintRectangle() {}
virtual void print() { std::cout << "\nprint PaintRectangle"; }
};
PaintShape* PaintShape::createShapeObject( std::string shape )
{
if( shape == "triangle" )
return new PaintTriangle;
else if( shape == "rectangle" )
return new PaintRectangle;
return new PaintShape;
};
class EndDeveloper
{
public:
EndDeveloper()
{
std::string shape;
std::cout << "\nWhat shape would you like? ";
// Get input from the terminal.
std::getline( std::cin, shape );
PaintShape *p = PaintShape::createShapeObject( shape );
p->print();
std::cout << "\nWhat shape would you like? ";
std::getline (std::cin, shape);
PaintShape *s = PaintShape::createShapeObject( shape );
s->print();
}
};
如何将其转换为工厂方法and/or抽象工厂方法?
工厂方法只产生一个项目,而抽象工厂产生一些相关项目的子集。
在你的例子中,你有一个模式工厂方法。
换句话说,关于如何记住某些内容的建议并不适用于每个人。对我来说,从报价中不清楚有什么区别:) 我建议您阅读以下书籍以将事物归位:
According to that explanation, is the following code a factory method or an abstract factory pattern?
该代码不是任一模式的实现:
- 对象创建不会延迟到子classes,所以它不是工厂方法的实现。
- PaintShape 没有一组工厂方法来加强关系,因此它不是抽象工厂。
What would be the way to convert this into a factory method?
假设我们必须使用工厂方法,我们必须引入一个class负责创建形状。更令人困惑的是,这些将属于 ShapeFactory 类型,但不要将其视为抽象工厂模式的实现。不是。1
在这里,我们的客户端通过调用其 createShape() 方法(工厂方法)与 ShapeFactory 进行交互。 Subclasses override 此方法和 return 正确的 Shape。这就是工厂方法模式的意义所在:将创建延迟到子classes.
#include <iostream>
class Shape {
public:
virtual std::string text() = 0;
virtual double area() = 0;
};
class Rectangle : public Shape {
public:
std::string text() override { return "rectangle"; }
double area() override { return 1.0; }
};
class Triangle : public Shape {
public:
std::string text() override { return "triangle"; }
double area() override { return 2.0; }
};
class ShapeFactory {
public:
virtual Shape* createShape() = 0;
};
class RectangleFactory : public ShapeFactory {
public:
Shape* createShape() override {
return new Rectangle();
}
};
class TriangleFactory : public ShapeFactory {
public:
Shape* createShape() override {
return new Triangle();
}
};
ShapeFactory* makeShapeFactory(std::string shape) {
if (shape == "triangle") {
return new TriangleFactory();
} else {
return new RectangleFactory();
}
};
int main() {
// Get input from the terminal.
std::string shape;
std::cout << "What shape would you like? ";
std::getline(std::cin, shape);
ShapeFactory* p = makeShapeFactory(shape);
Shape* s = p->createShape();
std::cout << s->text() << std::endl;
delete p;
delete s;
return 0;
}
I would be grateful if you could add the implementation of abstract factory method too.
ShapeFactory 现在可以创建二维或三维形状。现在我们有两个不相交的 class 层次结构:TwoDShape 和 ThreeDShape。但是,我们希望客户使用正确的 2D 和 3D subclasses。这里有一个隐含的关系,如果我们能在代码中明确说明会更好。这就是抽象工厂模式的意义所在:加强关系。
现在我们的 ShapeFactory 有两个工厂方法:create2dShape() 和 create3dShape()。当客户端与 ShapeFactory 交互时,它们会绑定到正确的 TwoDShape 和 ThreeDShape classes。例如,一个 RectangleFactory 工厂创建 TwoDRectangles 和 ThreeDRectangles。
#include <iostream>
class TwoDShape {
public:
virtual std::string text() = 0;
virtual double area() = 0;
};
class TwoDRectangle : public TwoDShape {
public:
std::string text() override { return "2d rectangle"; }
double area() override { return 1.0; }
};
class TwoDTriangle : public TwoDShape {
public:
std::string text() override { return "2d triangle"; }
double area() override { return 2.0; }
};
class ThreeDShape {
public:
virtual std::string text() = 0;
virtual double volume() = 0;
};
class ThreeDRectangle : public ThreeDShape {
public:
std::string text() override { return "3d rectangle"; }
double volume() override { return 2.0; }
};
class ThreeDTriangle : public ThreeDShape {
public:
std::string text() override { return "3d triangle"; }
double volume() override { return 4.0; }
};
class ShapeFactory {
public:
virtual TwoDShape* create2dShape() = 0;
virtual ThreeDShape* create3dShape() = 0;
};
class RectangleFactory : public ShapeFactory {
public:
TwoDShape* create2dShape() override {
return new TwoDRectangle();
}
ThreeDShape* create3dShape() override {
return new ThreeDRectangle();
}
};
class TriangleFactory : public ShapeFactory {
public:
TwoDShape* create2dShape() override {
return new TwoDTriangle();
}
ThreeDShape* create3dShape() override {
return new ThreeDTriangle();
}
};
ShapeFactory* makeShapeFactory(std::string shape) {
if (shape == "triangle") {
return new TriangleFactory();
} else {
return new RectangleFactory();
}
};
int main() {
// Get input from the terminal.
std::string shape;
std::cout << "What shape would you like? ";
std::getline(std::cin, shape);
ShapeFactory* p = makeShapeFactory(shape);
TwoDShape* s2 = p->create2dShape();
ThreeDShape* s3 = p->create3dShape();
std::cout << s2->text() << " and " << s3->text() << std::endl;
delete p;
delete s2;
delete s3;
return 0;
}
如前所述,抽象工厂是通过工厂方法实现的。这就是人们混淆它们的原因。然而,正是他们的 关注点 使他们与众不同。应用模式时,始终首先考虑他们的关注点,然后再考虑他们的实现。
1 参见 what's the advantage of using factory method pattern over simple factory?