1
\$\begingroup\$

I'm trying to figure out the best design here, and I can see multiple approaches, but none that seems "right."

There are three relevant classes here: Base, TradingPost, and Resource. Each Base has a TradingPost which can offer various Resources depending on the Base's tech level.

Where is the right place to store the minimum tech level a base must possess to offer any given resource? A database seems like overkill. Putting it in each subclass of Resource seems wrong--that's not an intrinsic property of the Resource. Do I have a mediating class, and if so, how does it work? It's important that I not be duplicating code; that I have one place where I set the required tech level for a given item. Essentially, where does this data belong?

P.S. Feel free to change the title; I struggled to come up with one that fits.

\$\endgroup\$
  • 1
    \$\begingroup\$ It sounds like you can just keep this as an attribute of the base: a list of resources, and a correlating list of the minimal base level before that resource appears. \$\endgroup\$ – ashes999 Jan 3 '13 at 16:20
1
\$\begingroup\$

IMHO, the responsibility of managing which Resources are available in a TradingPost should be in a separate controller-like class, TradingPostController for example.

TradingPostController observes changes of Base.techLevel and updates the TradingPost's set of offered Resources accordingly. In this approach, TradingPostController will contain a mapping of tech level -> resource set.

So the sequence of events would be:

  • Tech-level of Base is increased.
  • Base fires a "tech level changed" event.
  • TradingPostController observes the event.
  • TradingPostController updates TradingPost.resources (or so).

Good luck!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.