父类需要了解他的子类
问题:父抽象类必须了解其所有现有的子类。父类还必须能够通过一个方法列出他的子类的所有类名。
上下文:在我的情况下,父类用于表示 Web 表单的任何输入数据类型(例如电子邮件、文本、布尔值、整数、图像等)。子类代表每个特定的数据类型。因此有一个 Boolean 类、一个 Image 类、一个 Integer 类等。
我使用的语言是 C#。
你会怎么做?
编辑:我需要知道子类的原因是因为我需要知道所有可用的输入数据类型才能列出它们。另外,我需要访问每种数据类型(子类)的方法以获取其属性。
因此,例如,我需要简单地使用存储在数据库中的字符串“Image”来获取 Image 类的实例。如果基类知道它的所有子类,我将能够要求它获取由字符串“Image”表示的子类,并且它将返回 Image 类的实例。
我想通过使每个子项成为 Singleton 并将其自身添加到子项列表(在构造时)来实现它,该子项列表将是 DataType 基类的私有字段。
Probem: A parent abstract class must be aware of all his existing child classes. The parent class must also be able to list all the class names of his child classes through a method.
Context: In my situation, the parent class is used to represent any input data type for a web form (such as email, text, boolean, integer, image, and so on). The child classes represent each specific data types. So there is a Boolean class, an Image class, an Integer class, etc.
The language I'm using is C#.
How would you do that?
edit: The reason why I need to know the child classes is because I need to know all the input data types available in order to list them. Also, I need to access the methods of each data type (child class) to get its properties.
So, for example, I need to get an instance of the Image class simply with the string "Image" that is stored in a database. If the base class knowns all its children, I will be able to ask it to get me the child represented by the string "Image", and it will return an instance of the Image class.
I thought of implementing it by making each child a Singleton, and adding itself to a list of children (at construction) that would be a private field of the base DataType class.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以查看这篇文章。看起来您需要使用
Type.IsSubclassOf
迭代已知类型并查看它们是否是父类的子类You can check out this post. Looks like you need to iterate through known types and see if they are a subclass of the parent class using
Type.IsSubclassOf
您可以在构造函数代码中编写通知某些静态类已创建新内容的代码。
类似于:
myBuilder 是一个静态类,其代码:
编辑:
好吧,这个解决方案当然需要先实例化子类。
如果您想在程序启动时了解所有子类,我认为唯一的方法是扫描整个类路径并检查每个类是否是所需类的子类。
You can write in the constructor code that will notify some static class that something new was created.
Something like:
myBuilder is a static class with code:
edit:
Ok, this solution, of course, requires that the subclasses are instantiated first.
If you want to know all the subclasses when program starts I think the only way is to scan entire class path and check for each class is it subclass of wanted one.
让父母意识到自己的孩子是一种强烈的代码味道。它引入了一种令人讨厌的循环依赖。它尽可能公然地遵循依赖倒置原则(依赖于抽象,而不是具体)(使抽象类实际上依赖于其自身的具体实现)。它消除了作为继承核心的关注点分离。
简而言之,您应该进行重新设计。我必须更多地了解问题背景才能就此提供具体建议。
Making a parent aware of its children is a strong code smell. It introduces a nasty type of circular dependency. It treads on the Dependency Inversion Principle (depend on abstractions, not concretions) about as blatantly as possible (making an abstract class actually depend on concrete implementations of itself). It does away with the separation of concerns which is at the heart of inheritance.
In short, you should be in for a redesign. I would have to know more of the problem context to give specific advice on that.
很抱歉复活这个,但我认为您正在寻找的是一个了解所有实现的工厂,而不是一个了解其所有子级的基类。工厂基本上会知道它可以基于字符串构造的所有类型。
请原谅我,因为我不再熟悉 C# 语法,但它会类似于以下内容:
就像其他人评论的那样,父级了解其子级通常是一个非常糟糕的主意。
Sorry to resurrect this but I think what you are looking for is a factory that is aware of all of the implementations, not a base class that is aware of all of its children. The factory would basically know all types that it can construct based on the string.
Forgive me because I'm not too familiar with C# syntax anymore, but it would be something like the following:
Like other people have commented, a parent knowing about its children is typically a really bad idea.