From: Erik V. <eri...@hc...> - 2006-02-04 23:40:45
|
> > brett lentz wrote: > > >>Rather than checking it in every place, it seems like it > >>would be better > >>to have the Bank object (or whatever implements the bank) > >>keep track of > >>whether it is broken. Then at the end of an OR you can check > >>that flag > >>and end the game or give a warning message. > >> > >> > > > > > >Who said the return value needs to be checked everywhere? > > > >There are plenty of transactions already happening that don't check > >the return value and won't ever need to. > > > > > If you don't check it, you may miss a case where the bank > breaks. For > example, in one step you pay money from the bank and break > it. If that > one place doesn't check the return value, a later payment to the bank > might hide the fact that the result went negative. > > I am assuming I misunderstand what you intend since otherwise I can't > believe you are arguing this point. > > What I have in mind is a singleton class like this (and I > have no idea > how it is currently implemented): > > public class Bank { > int balance; > boolean broken; > > public Bank(int size) { > balance=size; > broken=false; > } > > public void Bank.adjustMoney(int amount) { > if(balance<amount) { > broken=true; > } > balance-=amount; > } > > public boolean Bank.isBroken() { > return broken; > } > }; > > It sounds like you are suggesting adjustMoney returns a flag and Bank > doesn't keep any state that it is broken, so it is up to all > the callers > to check the return value and propagate that information to anything > that needs to know it. Since there are very few places that need to > know the bank is broken (which will also need to check other game-end > conditions), it seems to make more sense to have the Bank object keep > track of whether it is broken and have those few things that > care check > for it rather than checking after every adjustment to the > bank balance. > I agree with John, which is how I would approach it. We also need a mechanism to inform the user that the bank has "just" broken (e.g. via a pop-up). That could be a method that returns true exactly once (Bank.isJustBroken()), but that method should then be called after every action. It's perhaps easier to have the Bank send out an Event or an Observer update call (I prefer the latter, as the mechanism is already being used). Erik. |