模式:通过将Object数组传递给构造函数来独立于不同的子类 [英] Patterns: Be independent of different subclasses by passing an Object array to the constructor

查看:83
本文介绍了模式:通过将Object数组传递给构造函数来独立于不同的子类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我从文件中加载了很多实体。这些实体中的大多数(并非全部)具有不同的类类型,因此可能(也可能没有)具有不同的构造函数。但是,它们都共享一个超类: Entity.class

Let's say I load a whole lot of entities from a file. Most of these entities (Not all of them) are of a different class type, and thus may (Or may not) have a different constructor. However, all of them share one superclass: Entity.class

让超类有多糟?有一个构造函数 public Entity(Object [] args); ,这样参数也将简单地从文件中加载并传递给构造函数,然后构造函数对这些参数进行排序

How bad/good is it to let the superclass have one constructor public Entity(Object[] args);, so that the arguments will simply also be loaded from file and passed to the constructor, where the constructor then sorts out what exactly to do with that array?

我什至想要做这样的事情的主要原因是因为我想避免巨大的切换语句,在这里我必须先检查一下

The main reason I even want to do something like this is because I want to avoid huge switch-statements, where I have to first check what class I am loading and then check the arguments as well.

基本上,假设我具有以下数据结构(假设键可以有重复项!)

Basically, let's say I have the following data-structure (Assuming keys can have duplicates!)

Map<String, ArrayList<String>>
     ^       ^         ^
EntityClass  Params  Parameter (Any type)

外观类似的XML文件:

Loaded from a similar-looking XML file:

<entities>
    <EntityTypeX>
        <parameter>ConstructorArg1</parameter>
        <parameter>42</parameter>
    </EntityTypeX>
    <EntityTypeX>
        <parameter>Whatever bro</parameter>
        <parameter>999</parameter>
    </EntityTypeX>
    <EntityTypeY></EntityTypeY>
    <EntityTypeZ>
        <parameter>myFile.png</parameter>
    </EntityTypeZ>
</entities>

然后我将以如下方式使用它:

I would then use it somehow like the following:

for each String className in my map-keys:
    Convert ArrayList to Object[]
    Get class of className, check if it is an entity:
        Invoke it's constructor with the object array

调用它的构造函数,因此每个实体类都可以像这样简单地工作: / p>

Each entity class could thus simply work like this:

public class EntityTypeX extends Entity {
    String myString; int myNumber;
    public EntityTypeX(Object[] args){
        myString = (String) args[0]; myNumbers = (Integer) args[1];
    }
}

我知道-我正在使用太多反射,再看整个设计,它看起来确实很糟糕。但是,我看到的唯一选择是使用类似的东西(仍然使用相同的数据结构和XML)

I know - I'm using way too much reflection, and, looking at the design of this whole thing, it does look quite bad. However, the only alternative I see is using something like this (Still using the same data-structure & XML)

Entity e;
switch className:
case "EntityTypeX": e = new EntityTypeX((String)objectArray[0], (Integer)objectArray[1]); break;
case "EntityTypeY": ...
case "etc": ...

这种结构的主要问题是:我无法使我的应用模块化。我不能简单地创建一个小型插件系统,允许我随时间插入新的实体类型,然后从新的XML中正确加载它们,因为我也必须更改此加载代码。我的目标是避免这样做!

The main problem I have with this kind of structure: I can't make my app modular. I can't simply make a small plugin system allowing me to plug-in new Entity Types with time, and then properly load them from a new XML, since I have to change this loading code as well. My goal is to avoid doing exactly that!

我也想避免使用反射。

那么...我该怎么办?或者,我可以做什么

So... What do I do? Or, what can I do?

谢谢!

推荐答案

这样做会非常可怕,令人沮丧。

It would be horribly, frustratingly bad to do that.

如果不查看类和类的实现,如何确定实际需要进入构造函数?

Without looking at your class and class implementation, how do you figure out what actually needs to go into the constructor?

您不知道,这在编程时令人沮丧。

You don't know, and that is frustrating when programming.

已显式

我强烈建议您查看鲍勃叔叔的书《清洁代码》 。对于我为什么建议命名所有参数,他将给出更好的解释。

I strongly recommend you to look at Uncle Bob's book Clean Code. He will give a much better explanation of why I recommend naming all your parameters.

这篇关于模式:通过将Object数组传递给构造函数来独立于不同的子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆