简体   繁体   English

在Java中实现此的最佳方法

[英]Best way of implementing this in java

So I have this requirments: 所以我有这个要求:

  • A class called Destination 称为目的地的类
  • A HashMap called Destinations 一个名为Destinations的HashMap

The Destination class has an array of objects of the type Door. Destination类具有Door类型的对象数组。 So basically I read a "door" from a file, and every door has an attribute "destination" which tells me which destination it belongs to. 因此,基本上我从文件中读取了一个“门”,并且每个门都有一个“目的地”属性,该属性告诉我它属于哪个目的地。

My question is, what's better?: 我的问题是,什么更好?

a) A method in the object that holds Destinations that checks whether the destination for the new door exists or not in the HashMap Destinations, and therefore insert that door to its existing Destination or create the new Destination and then insert the door. a)持有Destinations的对象中的一种方法,用于检查HashMap Destinations中是否存在新门的目标,然后将该门插入到其现有Destination中或创建新的Destination,然后插入该门。

b) Override (?) the add method for the Destinations HashMap and implement the previous described functionality there. b)覆盖(?)Destinations HashMap的add方法,并在那里实现前面描述的功能。

c) Another way. c)另一种方式。

Thank you. 谢谢。

In Java and similar languages we are more likely to create classes with meaningful names for our application than we are to have simple lists and maps and sets (as you would do in more dynamic languages). 在Java和类似语言中,与拥有简单的列表,映射和集合相比,我们更有可能为应用程序创建具有有意义名称的类(就像在动态语言中那样)。 What you will almost never see is someone subclassing HashMap or ArrayList or HashSet and overriding their add or put methods. 您几乎看不到的是有人将HashMap或ArrayList或HashSet子类化并覆盖其add或put方法。

The most "Java-esque" approach would be to define a class called Destinations which can contain (as a field) a hash map of destination objects, indexed by id. 最“ Java式”的方法是定义一个名为Destinations的类,该类可以包含 (作为字段)目标对象的哈希图,该哈希图由id索引。 You then will create only those methods that make sense to your application, like 然后,您将仅创建对您的应用程序有意义的方法,例如

add(door, destination)

which can encapsulate your game logic. 可以封装您的游戏逻辑。 No one needs to know there is a map (or list or set) behind the scenes. 没有人知道幕后有一张地图(或列表或集合)。 To expose the map would mean your application had a leaky abstraction, which you will want to avoid. 公开地图将意味着您的应用程序具有泄漏性抽象,您将希望避免这种情况。

Perhaps even better: It is also possible that what works best for you is to only have 也许更好:对您来说最有效的选择

class Door

and

class Destination

and make the map of all destinations be a field of Destination. 并将所有目的地的地图设为“目的地”字段。 Without knowing more of what you are trying to do, it's hard to say. 在不了解您要做什么的情况下,很难说。 Minimizing the number of classes seems like a good idea. 减少类的数量似乎是一个好主意。 Can you encapsulate your map into Destination, and make static methods to access all doors? 您可以将地图封装到Destination中,并使用静态方法来访问所有门吗?

Now if you decide to make a separate Destinations class, you would encapsulate your map like so: 现在,如果您决定制作一个单独的Destinations类,则将封装地图,如下所示:

class Destinations {
    private static Map<Integer, Destination> map = ...

    public static void add(Door door, Destination destination) ...
}

to make the call look like: 使呼叫看起来像:

Destinations.add(door, destination);

Alternatively, your destinations map can be a singleton. 另外,您的目的地地图可以是单身人士。 It's always fun to get opinions on the singleton vs. utility class question. 获得关于单例与实用程序类问题的意见总是很有趣。 See what works best for your application. 查看最适合您的应用程序的方法。

TL;DR: Hide the map inside of another class so clients don't know there's a java.util.HashMap using either: TL; DR:将地图隐藏在另一个类中,这样客户端就不会使用以下任何一种方法来获取java.util.HashMap

  • a utility class Destinations with static methods 带有静态方法的实用程序类Destinations
  • a singleton class DestinationInfo 单例类DestinationInfo
  • the map hidden as a static field (with static methods) inside the model object Destination itself. 映射隐藏在模型对象Destination本身内的静态字段(使用静态方法)中。

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

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