简体   繁体   English

Map.java.lang.Object的替代方法

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

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. 因此,我必须构建一个新的Java应用程序,并且有一种情况,我倾向于在周围使用所有对象的对象映射。 I just don't like this idea though, but can't figure out a better solution. 我只是不喜欢这个主意,但却找不到更好的解决方案。

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. 这就是Map进入的地方,它将是验证者的输入,因为他们知道要使用地图的哪个键。

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通用库中的Netty的AttributeMap

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM