views:

140

answers:

2

Hi all,

So this question is a sort of follow on from here (http://stackoverflow.com/questions/1914097/how-to-deal-with-multiple-event-args). That question led me onto thinking about this but is different enough to warrant its own thread.

I am creating a game (for fun and learning purpose) and would like to know if I am using good standards for design or not. I think I might have gone OTT on separation of concerns, or just got the whole thing wrong, but I am hoping that isn’t the case. I have no problem rewriting it as I want to learn “best practices” and the practical application of them.

EDIT

Let me explain a little more about the game, It is based on Jawbreaker a game found on many mobile phones (quick demo found here). Objective is to select balls that are in groups to remove them from play and score as many points as possible.

I am trying to expand it a little and have different types of boards, balls move in different ways, and different ball types, the balls may just go where they are told to, or they might do something along the way.

So Here is the structure of the objects I have created, top row shows the DLL’s with their Objects, second row shows what those objects reference:

alt text

Here is my attempt at doing the UML:

alt text

Click here to link to full page for UML, makes it a little larger and hopefully easier to read

The Objects DLL holds the basic objects used in the game, Balls and a Board. They do not contain any logic about how they act / react to situations (the ball does implement the CompareTo and the Equals methods). There could be X number of implementations of IBall (and the same for IBoard, although there will not be that many I imagine).

The InstanceManager DLL is used as a way of Creating Objects, not sure this was 100% needed it could have gone in the Objects DLL. The Factories are static classes that have various overloaded methods to create IBall objects. The BallFactory can take the BallType Enum, A Drawing.Color object, etc. The BoardFactory is very similar. Jawbreaker is a singleton object that deals with things like holding a Random Object as it is used very regularly and some GameConfiguration data, (not so relevant to this topic).

The Engine DLL is where most of the work happens. The LogicFactories take the BallType and BoardType objects to create the relevant Logic Objects. The logic objects are used to control how an IBall and IBoard object works. The BallLogic tells the ball what it can do when its events fire. For example when a Ball is selected a method is called on the Ball Logic saying Ball X on Board Y has been selected. The Ball can then do whatever its type of ball should / could do. The BoardLogic is very similar and deals with how the board acts.

The Engine Object is another singleton and is how the GUI would interact with the whole game. The GUI would not instantiate any of the other objects directly.

So to summarize the IBall and IBoard classes hold just the data about them, the Logic classes deal with all the functionality.

What I would like to know is:

1) Is this a sensible approach?

2) Should (in general) Logic be separate from the Objects / data?

3) Have I gone too far with the separation of concerns?

4) Any other comments about the design / structure

EDIT

The reason I have used a couple of singletons is partly for simplicity of accessing the data in one place without keeping hold of objects all the time and also just because it is a single game and will not be scaled to either high end or across multiple machines. I do understand that they are not great and its not something I use regularly, but appreciate the comments.

Thank you for your thoughts and feedback.

+2  A: 

Your breakdown looks like it is going in the right direction. However I'm not sure you are trying to seperatate the data from the presentation from the logic. Take a look at the MVC, observer, and strategy patterns.

Keep this in mind: There is a tradeoff for when you are designing an application to be loosely couple. You get the ability to extend the application with less effort, however you do loose performance and memory usage. Also, I hate to say this, but don't create unnecessary interfaces. If you know you are going to extend the functionality of the object now or later on create the interface, if not think about it before you implement it.

monksy
Will look at the MVC Observer and strategy patterns, I see what your saying about the Interfaces, in this case almost all are used by a number of classes straight away. thx
Jon
Looks like I half walked down the Strategy road without knowing it. I am going to refactor things so I do it correctly. Makes complete sense, then I would just have a Ball object that would have different strategies
Jon
That would make sense.
monksy
+1  A: 

It's hard to critique your design without a better idea of what you are trying to do. I know that it involves balls and boards, but other than that, I have no idea.

Try to avoid singletons as much as possible. Yes, I know that the GoF book lists it, and I know that it is a common pattern, but in my experience it ends up becoming an anti-pattern.

You mention that IBall and IBoard don't have any logic about how they interact. Does that imply that they don't have any methods? Are their methods just getters and setters for their private data? If IBall and IBoard are just structs (or their equivalent), then they don't need to be interfaces. Could you flesh out your description to talk about the kinds of messages that you can send to an IBall and to an IBoard?

2) Should (in general) Logic be separate from the Objects / data?

Sometimes. If you have plain old data, then separating the logic from the plain data is fine. Of course, then you're not doing OOP anymore - you are writing procedural code. I think you're struggling with the desire to not hard-code any behavior into Ball. Perhaps some other entity should be responsible for deciding how balls interact, but you still have room for Ball to participate in the decision. This is where the strategy pattern (among other patterns) would come into play. Think about how things work in the real world - how would a physical ball and a physical board coordinate their interactions? If they talked to each other, what would they say? See if you can implement that in your code.

Finally, a word about design. I think it's good to want to "get the design right." On the other hand, that's the path towards becoming an architecture astronaut. Consider what problems you currently have with your code base.

  • Is it hard to refactor?
  • Is it hard to add new types of things?
  • Is too much "hard-wired"?

Design doesn't exist in a vacuum - it is informed by the current state of the world. Just look at some of the crazy cell phone concept art that people come up with, and you'll see good examples of bad design. Cell phones are limited by current technology, and designs that ignore the restrictions of the real world are nothing more than dreams. The same is true in software. Pay attention to what the code is telling you it needs, and you'll do fine.

The more software you write (and finish), the more experience you will have, and the better your sense of aesthetic will become. Don't let fear of "doing it wrong" stop you from finishing your software. Persevere, make it work, and then see what you learned from the process.

Daniel Yankowsky
Thank you for your great response. I am going to add some extra into the question to expand it.
Jon
I am thinking that the approach I have taken is not the best idea. I see why I went that way and think that if I just step back from it the strategy pattern would be a better approach. putting that into the Ball and Board means I remove the inheritance there, and get rid of a whole DLL of bits and bobs. Thx
Jon
I think I see what you mean. From your UML, it looks like your board has a BoardType field, and your ball has a BallType field. My guess is that these are enums, and that you use an if or switch statement somewhere in your code to do different things depending on the BoardType field. This is typically the perfect case for a strategy pattern. The strategy is an interface, and you will have one implementation for each enum value. The enum goes away. This pushes the decision making from explicit "if" tests into the land of polymorphism.
Daniel Yankowsky
HI Daniel, Your right they are Enum's that are used in the FActories to decide which types of object to create (switch statement). I can't see you completely get rid of the Enum though. If I just have a single Ball object that just has different IBallStrategy's, doesn't the BallFactory still need the enum (or something) to decide what strategy to use when creating the ball?
Jon
Was Xmas shopping in town and realized where I am going wrong... I still need an IBall and the various implementations and each class explicitly says what its strategy is. My only issue is how I create a random ball, I think I would need to use reflection to get all the ball types and randomly choose one.
Jon
Yes, you would still need the enums for the Factories. I'm sorry that I wasn't clearer. Ideally, the factory would take the enum value, create an instance of a strategy, create an instance of Ball (and pass the instance of the strategy to the ball's constructor), and return the ball. Unless, of course, you could get rid of the factories... :)
Daniel Yankowsky
The idea behind the strategy pattern is to hide different behaviors behind a common interface. Applied correctly, you would have a Ball class which would have a field of type IBallStrategy. There would be two implementors of IBallStrategy: StandardBallStrategy and PoppingBallStrategy (of course, you can add more). When you construct a ball, you pass the strategy to the constructor. Ball's methods then call into the strategy as necessary. Ball only contains the behavior that is common to all balls. It needs to consult its strategy to make more specific decisions.
Daniel Yankowsky