Game Development Reference
In this example, the client is sending the bank's `alkoep setter the value of its [_da_gR]hqa (which is
-,,). The setter first checks to make sure that it's actually being sent a value that would be appropri-
ate to deposit. (Hey, these clients are sneaky, it has to check everything!) If it seems okay, the value
is deposited into the bank's [_heajp=__kqjpR]hqa property. Like getters, setters are also defined as
Figure 8-4 illustrates this relationship.
2.The Bank temporarily
stores 100 in a local
variable called money.
The Client Class
The Bank Class
>]jg* `alkoep 9[_da_gR]hqa7
1. The Client sends the
Bank's setter the value
of _checkValue, which
lq^he_op]pe_bqj_pekjoap `alkoep $ikjau6ejp%6rke`
3. If the amount seems acceptable, the
Bank stores it in a private property called
_clientAccountValue. Its value is now 100.
Figure 8-4. Setters allow classes to receive information from other classes and use that information to change
their properties if the conditions seem fair.
The wonderful thing about using getters and setters is that objects can communicate without having
to directly access or change each other's properties. This helps with encapsulation, which means that
the classes are completely sealed off from one another. If they have to communicate, they do so only
under strict conditions. If you use getters and setters in your own classes, as in these examples, you're
far less likely to encounter bugs that come from objects not being able to make sense of information
they weren't programmed to deal with.
Getting started with the object-oriented approach
All you really need to know to get started using object-oriented programming are the techniques and
concepts discussed over the last few pages:
Think of each class as a model of an object. Describe the object using properties and
Make all your properties and methods private. If ?h]oo= tries to directly change or access the
properties in ?h]oo>, you'll begin to weave a tangled web of dependencies that could be very
difficult to debug if things go wrong.
Your classes should communicate with each other using getters and setters.