替代 Map of java.lang.Object [英] Alternative to Map of java.lang.Object

查看:36
本文介绍了替代 Map of java.lang.Object的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我必须构建一个新的 Java 应用程序,并且有一个场景,我倾向于使用带有强制转换的对象映射.我只是不喜欢这个想法,但想不出更好的解决方案.

So, I have to build a new Java application and I have a scenario where I'm inclined to use a Map of Objects with casts all around. I just don't like this idea though, but can't figure out a better solution.

场景如下:我有一些用户输入以及一个定义要采取的操作的标签.首先,基于标签,用户输入将被丰富一个或多个对象,然后将其传递到验证器机器进行处理.这些验证器有些通用,因此它们可用于一个或多个标签.这就是 Map 的用武之地,它将是验证器的输入,因为它们知道要使用 Map 的哪个键.

Here's the scenario: I have some user input along with a tag that defines the action to be taken. First, based on the tag, the user input will be enriched with one or more objects, and then it will be passed along to a validator machine to be processed. These validators are somewhat generic, so they can be of use to one or more tags. That's where the Map comes in, it will be the validators input, because they know which key of the map to use.

我一直在研究命令链设计模式,但它使用单个请求,在我的情况下,请求可能因标签而异.此外,我的验证器机器已经实现了它的主要目的.

I have been looking at the Chain of Command design pattern, but it uses a single request, and in my case the request can vary depending on the tag. Also, it's main purpose is already achieved with my validator machine.

有没有更好的解决方案?

Is there a better solution for this?

推荐答案

如果您不想编写自己的类型安全异构容器,请考虑 Netty 的 AttributeMap,可在 netty-common 库.

If you don't want to write your own typesafe heterogeneous container, consider Netty's AttributeMap, available in the netty-common library.

这篇关于替代 Map of java.lang.Object的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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