构造函数的巨型 switch 语句
我有一个容器,其中包含一堆指向基类的指针,以及一个接受一些输入并返回一个作为基类的子类的类的函数。它返回哪个子类取决于输入。
现在,我有一个像这样的巨大 switch 语句:
class Base { ... }
class A : public Base { ... }
class B : public Base { ... }
...
class Z : public Base { ... }
Base* depends(int input) {
switch (input) {
case 1:
return new A(...);
case 2:
return new B(...);
...
case 26:
return new Z(...);
default:
...
}
}
我想知道是否有更好的方法来设计它。我不知道很多“设计模式”(我认为这就是它们的名字),所以我不知道是否有(明显的)更好的方法来设计它。
I have a container which holds a bunch of pointers to a base class, and a function which takes some input and returns a class which is a subclass of the base class. Which subclass it returns depends on the input.
Right now, I have a giant switch statement like this:
class Base { ... }
class A : public Base { ... }
class B : public Base { ... }
...
class Z : public Base { ... }
Base* depends(int input) {
switch (input) {
case 1:
return new A(...);
case 2:
return new B(...);
...
case 26:
return new Z(...);
default:
...
}
}
I was wondering if there's any better way to design this. I don't know many "design patterns" (I think that's what they're called) so I don't know if there's a (obvious) better way to design this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您正在寻找的是工厂方法模式。
这里重要的是消除基类对派生类实现的任何了解的需要。对于基类来说,了解派生类的知识是一个糟糕的设计。
工厂方法模式解决了上述问题,因为创建发生在基类之外。
What you are looking for is an Factory Method pattern.
The important thing here is to remove the need for the Base class to have any knowledge of the derived class implementations. It is a bad design for a Base class to have knowledge about Derived classes.
Factory Method pattern addresses the above problem as the creation occurs outside of the Base class.
弄清楚你的意图有点困难,但如果你想根据某些输入参数创建一堆不同的子类,你可能需要考虑抽象工厂模式。
Its a little hard to work out what you're intending with this, but you might want to consider an Abstract Factory pattern if you want to create a bunch of different subclasses based on some input parameter.
另一种方法是创建一个数组,在其中放置指向将调用相应构造函数的函数的指针。在你的depends()中,你只会通过给定的输入调用你需要的函数。但无论如何,这种方法都需要 26 个函数
another way is to create an array where you will put pointers to the functions that will call corresponding constructor. And in your depends() you only will call the function that you need by given input. But any way you need 26 functions in this approach
整数参数“输入”来自某处。您也许可以让创建该 int 的代码创建实际的对象。如果您从磁盘或类似的东西读取 int ,那么这将不起作用。
您可能会考虑设置一种情况,其中不同的子类将自己注册到创建它们的对象。在这种情况下,工厂对象在编译时不需要知道子类。您可以在启动时使用全局变量来完成此操作,全局变量的构造函数为每个子类进行注册。您的 switch 语句更简单、更快,但这确实意味着您必须在更改子类时使 switch 保持最新。这是一种权衡,我认为您的解决方案不一定不如更复杂的解决方案。
The integer parameter "input" comes from somewhere. You might be able to let the code that created that int create the actual object instead. That won't work if you are reading the int from disk or something like that.
You might consider setting up a situation where the different subclasses register themselves with the object that creates them. In that case the factory object wouldn't need to know about the subclasses at compile time. You can have this done at start-up time using global variables whose constructors do the registering for each subclass. Your switch statement is simpler and faster, but it does mean you have to keep the switch up to date as you change the subclasses. It's a trade off and I don't think your solution is necessarily inferior to a more elaborate one.