Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Who should a method belong to?

I'm trying to design a simple game ---Pong, Arkanoid--- using strictly "Proper OO", whatever that means.

So, by designing myself to death, I'm trying to get to a point where I'll know what to do or not do next time...

Who should handle, for example, colissions? And scorekeeping?

My first idea was giving a couple jobs to the ball, but that started expanding exponentially into a god object: The ball'd know where it is, who it crashed into, and report to the scorekeeping objects.

And that's too much for the poor ball.

like image 962
Tordek Avatar asked Mar 13 '09 04:03

Tordek


People also ask

Do methods belong to class?

Class methods and Class variable are the static members of the class, which belongs to the class, they are being shared by all the objects of the class. 2. non-static variables and methods in a class , belongs to the objects. Every object has their own of these non-static members.

Are methods members?

A "method" is a particular type of member, described by MSDN as such: Methods define the actions that a class can perform.

What is a method and why do we need method?

A method works for the object from which is named after. A method can have one function to create its action, or several functions to create a complete solution. So in a way, methods and functions are not the same thing since a method can have one or more functions.

Why do we call a method?

It is widely used because it provides reusability of code means that write once and use it many times. It also provides easy modification. Each method has its own name by which it is called. When the compiler reads the method name, the method is called and performs the specified task.


Video Answer


4 Answers

rule of thumb:

  • if an object logically owns all of the state involved, then it owns the methods that use that state
  • if a method requires state from more than one object:
    • if a container object owns all of the objects used by the method, then it owns the method, too
    • otherwise you need a 'bridge' object to own the utility method
    • unless there is a strong argument for one object to be the 'controller' for the method (can't think of an example offhand though)

in your case, the 'gameboard' or 'game environment' would probably have a 'game physics' class (or group of methods) that owned the collision-detection (utility/bridge) methods

like image 104
Steven A. Lowe Avatar answered Oct 23 '22 03:10

Steven A. Lowe


A good or bad design reveals itself by how well it accomodates unexpected requirements, so I would suggest keeping a stock of potential "game features" handy to inform your design reflexions. Since you're doing this as a learning project you can afford to go crazy.

Arkanoid is a very good choice for this, it offers so many options. Make different bricks score different amounts of points. Make some bricks change the score of other bricks when hit. Make some bricks require multiple hits. Give superpowers to the ball, paddle, or bricks. Vary these powers: one of them makes the ball keyboard-controllable, another makes it transparent, another reverses "gravity", and so on. Make bricks drop objects.

The goal is that when you make such a change, it impacts the minimum possible number of classes and methods. Get a feel for how your design must change to fit this criterion.

Use an IDE that has a Refactoring menu, in particular the move method refactoring. (If you haven't, read the book Refactoring.) Experiment with placing your various methods here and there. Notice what becomes hard to change when the method is placed "wrong", and what becomes easier when you place it elsewhere. Methods are placed right when objects take care of their own state; you can "tell" an object to do something, rather than "ask" it questions about its state and then make decisions based on its answers.

Let's assume that in your design each sprite is an object instance. (You could choose other strategies.) Generally, motion alters the state of a sprite, so the method that describes motion for a particular kind of sprite probably belongs on that sprite's class.

Collision detection is a sensitive part of the code, as it potentially involves checking all possible pairs of sprites. You'll want to distinguish checking for collisions and informing objects of collisions. Your ball object needs to alter its motion on colliding with the paddle, for instance. But the algorithm for detecting collisions in general won't belong on the ball class, since other pairs of objects may collide with consequences that matter to the game.

And so on...

like image 21
Morendil Avatar answered Oct 23 '22 04:10

Morendil


Keep in mind that objects can also exist for logical elements of a given problem (not only real elements, like balls and boards).

So for collision detection, you could have a CollidingElement class that handles the position and shape states. This object can then be embedded by composition in any object that should collide in the game and delegate any needed method call to it.

like image 25
jturcotte Avatar answered Oct 23 '22 03:10

jturcotte


It really depends on your implementation, but I imagine you'd have a "gameboard" object to manage score keeping, or maybe a goal object on each side. As far as collisions I think you might want to pass events between the objects. I think any object should know its location though.

like image 41
Jeff Dickey Avatar answered Oct 23 '22 04:10

Jeff Dickey