implementation - Best way of implementing this in java -


so have requirments:

  • a class called destination
  • a hashmap called destinations

the destination class has array of objects of type door. read "door" file, , every door has attribute "destination" tells me destination belongs to.

my question is, what's better?:

a) method in object holds destinations checks whether destination new door exists or not in hashmap destinations, , therefore insert door existing destination or create new destination , insert door.

b) override (?) add method destinations hashmap , implement previous described functionality there.

c) way.

thank you.

in java , similar languages more create classes meaningful names our application have simple lists , maps , sets (as in more dynamic languages). never see subclassing hashmap or arraylist or hashset , overriding add or put methods.

the "java-esque" approach define class called destinations can contain (as field) hash map of destination objects, indexed id. create methods make sense application,

add(door, destination) 

which can encapsulate game logic. no 1 needs know there map (or list or set) behind scenes. expose map mean application had leaky abstraction, want avoid.

perhaps better: possible works best have

class door 

and

class destination 

and make map of destinations field of destination. without knowing more of trying do, it's hard say. minimizing number of classes seems idea. can encapsulate map destination, , make static methods access doors?

now if decide make separate destinations class, encapsulate map so:

class destinations {     private static map<integer, destination> map = ...      public static void add(door door, destination destination) ... } 

to make call like:

destinations.add(door, destination); 

alternatively, destinations map can singleton. it's fun opinions on singleton vs. utility class question. see works best application.

tl;dr: hide map inside of class clients don't know there's java.util.hashmap using either:

  • a utility class destinations static methods
  • a singleton class destinationinfo
  • the map hidden static field (with static methods) inside model object destination itself.

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

.htaccess - Matching full URL in RewriteCond -