简体   繁体   中英

Best way of implementing this in java

So I have this requirments:

  • A class called Destination
  • A HashMap called Destinations

The Destination class has an array of objects of the type 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.

b) Override (?) the add method for the Destinations HashMap and implement the previous described functionality there.

c) Another way.

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). What you will almost never see is someone subclassing HashMap or ArrayList or HashSet and overriding their add or put methods.

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. 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?

Now if you decide to make a separate Destinations class, you would encapsulate your map like so:

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:

  • a utility class Destinations with static methods
  • a singleton class DestinationInfo
  • the map hidden as a static field (with static methods) inside the model object Destination itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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