在 Java 中将内核模型转换为输出模型
这里我有一些代码可以将内部核心模型“传输”到外部插件的“输出”模型。
为此,我根据传递的 OldConnection 的具体子类型创建新实例,并将旧实例传递(通过强制转换)到构造函数中,这样我就可以轻松维护 getter 和 setter 的内部数据。
因此,OldIncoming、OldOutgoing 和 OldExpected 是 OldConnection 的子类型。 MyNewIncoming、MyNewOutgoing 和 MyNewExpected 是 MyNewConnection 的子类型。不幸的是,我无法更改内核模型,并且我需要构造函数中的具体类型。
代码看起来很丑陋,但我只是找不到更好的解决方案,有什么想法吗?
private MyNewConnection createIConnectedSubtypeInstance(OldConnection connection) {
if (connection instanceof OldIncoming){
return new MyNewIncoming((OldIncoming) connection);
}
if (connection instanceof OldOutgoing){
return new MyNewOutgoing((OldOutgoing) connection);
}
.
.
.
if (connection instanceof OldExpected){
return new MyNewExpected((OldExpected) connection);
}
return new MyNewConnection(connection);
}
Here i've got some code to "transfer" an inner core model to an "output" model for external plugins.
For doing this, I create new instances based on the concrete subtype of a passed OldConnection and pass (with a cast) the old instance into the constructor, so i can easily maintain the inner data for getters and setters.
So, OldIncoming, OldOutgoing and OldExpected are subtypes of OldConnection. MyNewIncoming, MyNewOutgoing and MyNewExpected are subtypes of MyNewConnection. Unfortunately, I cant change the inner core model and I need concrete types in the constructors.
Code looks pretty ugly, but I just cant find a better solution for it, any ideas?
private MyNewConnection createIConnectedSubtypeInstance(OldConnection connection) {
if (connection instanceof OldIncoming){
return new MyNewIncoming((OldIncoming) connection);
}
if (connection instanceof OldOutgoing){
return new MyNewOutgoing((OldOutgoing) connection);
}
.
.
.
if (connection instanceof OldExpected){
return new MyNewExpected((OldExpected) connection);
}
return new MyNewConnection(connection);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
另一种选择是一堆重载方法,如下所示:
但是,只有调用者知道
connection
的类型时,这种方法才有效,否则您必须依赖那些instanceof< /代码> 检查。
在这种情况下,您还可以进行一些映射 OldClass->NewClass 并使用反射来创建实例,但我怀疑这值得这么麻烦,除非您需要大量的映射。
示例:
请注意,这依赖于直接映射的
connection
类。如果您映射超类,则可能必须检查这些超类是否找不到connection
的实际类的映射。此外,这依赖于新实例的构造函数只接受映射类类型的一个参数。
An alternative would be a bunch of overloaded methods, like this:
That, however, works only if the caller knows what type
connection
is of, otherwise you'd have to rely on thoseinstanceof
checks.In that case you could also have some mapping OldClass->NewClass and use reflection to create instances, but I doubt that would be worth the hassle unless you need a huge amount of mappings.
Example:
Note that this relies on the class of
connection
being mapped directly. If you map super classes you might have to check those if no mapping for the actual class ofconnection
can be found.Additionally, this relies on the constructors of the new instances to accept exactly one parameter of the mapped class type.
您可以重载您的工厂方法:
正如托马斯在他的回答中指出的那样,只有当您使用正确的静态类型调用这些方法时,这才有效:
由于重载使用静态绑定,因此您不能在 this 中使用
OldConnection
案件。如果这不适合您,那么在某些时候您将不得不执行instanceof。You could overload your factory method:
As Thomas points out in his answer this will only work if you call these methods with the correct static type:
As overloading uses static binding, you cannot use
OldConnection
for a in this case. If this is not an option for you, then you will be stuck with doing instanceof at some point.您可以将包装器的创建移至
OldConnection
及其派生类。在OldConnection
中,定义了方法In
OldIncoming
override with和 in
OldExpected
override with这样,您就可以摆脱分支和强制转换。
You can move the creation of the wrapper to
OldConnection
and its derived classes. InOldConnection
, defined the methodIn
OldIncoming
override withand in
OldExpected
override withIn that way you get rid of the branches and the casts.