简体   繁体   中英

How can I reorganize my code into classes in C#?

I'm working on a project in C# w/ XNA, and I want to reorganize it and divide parts of it into classes.

At present, it's just one game.cs file, but I want to clean it up a bit. Unfortunately, I'm not too familiar with classes. Perhaps someone can help me figure out how to do this, or direct me to some tutorial of some sort?

Well, start off by thinking of which bits of data go together naturally. For example, if you have a player's name, score, position, number of lives etc, that would suggest you should have a Player class. So take a look at all the state, split it up into different groups, and then check which of your existing methods only work with one group of state or another... and put the methods with the state they're interested in.

It gets trickier when one method needs to deal with state from multiple objects, of course: typically that means you'll put the code in one type and pass the other type in as a parameter.

A lot of this is gut feeling, to be honest - the above are only starting points. Over time it gets somewhat easier, and you'll probably start to think more in terms of responsibilities than just data - but it's a good starting point, as you probably already have the data you need, just not organized in the right way... whereas deciding on the responsibilities within the system can be a lot harder.

There's a useful principle in software design known as "Zero, One, Many".

  • Either you have zero of something - so you don't bother implementing it in the first place
  • Or you have one of something - you probably don't need to make this a class.
  • Or you can have many of something - you probably should make this a class.

(The important take away is that there's no case for having two, or three, or four of something.)

Looking at your website, I see you are making a Pong clone. You've got two paddles - so this falls under the "many" case - so make yourself a Paddle class. This way you can create two instances of the class, one for each player.

This way each paddle shares the same functions, but uses different data. Reducing code duplication (which is a good thing).

You've also got one ball. Whether you make this a class or not is probably a matter of taste (and what kind of ambitions you might have for adding a "multi-ball" mode). I would suggest not making this a class, until you find you have a need to do so.

Classes are a pattern for an object that can be created, an abstract description of an entity. When a class is instantiated and populated, it then contains the data that describes that entity.

An object typically should encapsulate one literal "thing", or be responsible for managing one specific set of functions.

http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod

A good example would be a class that represents a playing field, or a class that provides helper functions for a specific type of math operation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM