I'm undecided as to what classes I could have that could adapt to an existing system which is an online video game. Here's what I want to achieve:
- Get a series of settings from objects in the server.
- Listen for clients to connect.
- For each client, check that开发者_如何转开发 the settings on the client correspond with those from the server.
- If settings don't correspond (something has been tampered with), either disconnect the client or change their settings.
This will be handled by class that will act as an entry point and can serve as a form of controller.
Now, the settings are strewn accross a number of instances: players, weapons, flags, lights, etc. In procedural programming, I'd get all this information and store it an array. However, is there a better way of doing this according to an OO approach? Can I make one or more classes that will have the values of these settings and act as a form of facade?
Encapsulate the settings data and behavior into at least one object (i.e. Settings
). Depending on how your system is constructed this becomes part of other objects' composition (e.g. Player
, Weapon
, etc...), perhaps via dependency injection, or referenced from some global context. Settings
is responsible for validation the match between client and server (e.g. Settings.validateClientServerSettingsMatch()
). In terms of retrieving individual settings, two possible approaches explicit or implicit.
Explicit
Your Settings
object, or perhaps other entities that make its composition, have methods for each setting managed. So it could be something like Settings.getPlayerSettings().getSomeSetting()
or 'Settings.getSomePlayerSetting()`. How nested really depends on your system. Either has the advantage of making clear what settings are available to the client development and it procides compile time type checking if you're using a language such as Java. The tradeoff is needing to alter an object every time a new setting comes into play.
Implicit
This just has a generic method in the Settings
object - Settings.getSetting(settingName)
. This makes it very easy to add settings, at the expense of any sort of useful type checking, unless you do something on your own using some meta magic of sorts in a language such as Python or Ruby or large case statements in Java.
精彩评论