Game Development Reference
In-Depth Information
Wow, I hope that's not my bank! Now let's create an imaginary client who wants to access as much of
that many as she can:
lq^he__h]oo?heajp
w
lq^he_r]njaa`oPkL]u>ehho9pnqa7
lq^he_r]nd]oIkjau9b]hoa7
lq^he_bqj_pekj?heajp$%
w
>]jg*ceraIkjauPk?heajp9pnqa7
y
y
The client has just robbed the bank! What? You missed it? Look again:
>]jg*ceraIkjauPk?heajp9pnqa7
Even though the client had no money in her account, and the bank was instructed not to give her
any, there was nothing stopping her from taking as much as she wanted with this one simple directive.
Figure 8-2 illustrates how this works.
The Client class
The Bank class
lq^he__h]oo?heajp
w
lq^he_r]njaa`oPkL]u>ehho9pnqa7
lq^he_r]nd]oIkjau9b]hoa7
lq^he__h]oo>]jg
w
lq^he_r]n_heajpD]oIkjauEj=__kqjp9b]hoa7
lq^he_op]pe_r]nceraIkjauPk?heajp9b]hoa7
y
lq^he_bqj_pekj?heajp$%
w
>]jg*ceraIkjauPk?heajp9pnqa7
y
y
Figure 8-2. Because the bank's giveMoneyToClient variable is public, the client can change it to true on a whim,
and the bank has no way of stopping it.
You want to avoid objects directly accessing the properties of other objects because you could acci-
dentally write some code that could destroy the economy of your video game world. Even though it's
a little more effort, there is a much safer way to write the code for these two objects.
Let's try again, starting with the ?heajp class:
lq^he__h]oo?heajp
w
lner]par]n[jaa`oPkL]u>ehho9pnqa7
lner]par]n[d]oIkjau9b]hoa7
lq^he_bqj_pekj?heajp$%
w
 
Search Nedrilad ::




Custom Search