-3

I wrote abstract base for some turn-based games (like chess or tic-tac-toe) and wrote game based on this base. And I stuck with choosing how to design class hierarchy. Here is two variants for wich I came up: Static class wrapping And here is second screenshot (it is too long to post here as image)

In first variant all classes in different namespaces (or I can move them to one namespace). In second variant all classes separated with static classes and they all in one namespace. First variant's diagram looks better, but I think that second is more correctly. How better to design this structure?

7
  • 1
    I'm not sure why you are messing up with namespaces, but it seems to me that you're not grasping what they are meant for. Your class diagrams - both of them - seem a bit off. You are using nested types for things that shouldn't be nested types. Second diagram is definitely not more correct - it is actually way worse than the first. Commented Jan 6, 2017 at 10:27
  • Base your work on your first diagram, but get rid of those nested types and make them their separated classes. Also, the only class you have that really could be static is your "Tools" class. If you have a base for turn-based games, move it to another visual studio project - it will improve re-usability and will force you to think a little more about your design choices. Commented Jan 6, 2017 at 10:32
  • @ThalesPereira, like this? Commented Jan 8, 2017 at 4:58
  • Oh, way better! But again, the only classes that need to be static are the "Tool" ones. You still have two other classes that doesn't need that Static modifier there. You should also take a look on something called Dependency Injection. It's something that's pretty cool for things that implement changeable rules. It's not as specific as a strategy pattern (it's a more general approach), but can give you a lot of help here. Commented Jan 9, 2017 at 10:25
  • Only after second comment I finally understood why there is no need in those static classes. So far I have not had time to understand what is dependency injection, but here is new version of my diagram. Is there another comments about my diagram, except of using dependency injection? Commented Jan 10, 2017 at 0:37

1 Answer 1

0

Well, first things first :)

You don't really need to mess with namespaces on your case. Namespaces are more of an organizational tool than anything else. Most of the time, tho, you can just leave this up to Visual Studio. The IDE is really smart and will figure out the namespace name you should use based on the folder structure you have on your project.

Next, avoid using nested types. Nested types are OK for a small set of necessities, not on your case. You don't really need the "Base" class - just promote your nested types to regular classes and you're good to go.

Also, you don't need for those non-tool classes to be Static. They look more things that would belong to a single instance of a board than to the entire app. What would you do if you wanted to have two chess matches going side by side? As a rule of thumb, leave the "globalness" of the static keyword for things that should really, really be global. Your "tools" classes seem like likely candidates for the static keyword, so you can leave that there.

As an alternative, you can look up Dependency Injection. While it seems like a big, scary concept, is something so simple most of people where already doing it anyway by hand way before DI frameworks were a thing. Don't be tricked, tho - Dependency Injection doesn't need to, and sometimes should not, be used by means of a DI Framework. You can very well do it by hand without issues and without the added complexity of a DI Framework.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.