Re: OO is not that great[2]: repeatedly passing the same object
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Shawn schreef:
Hi,
I have several classes TellerA, TellerB, TellerC and BankAccount.
TellerA and TellerB and TellerC are working like in a chain. TellerA
does something to the bankaccount object and pass it to TellerB. TellerB
does something to the same bankaccount object and pass it to TellerC.
There is only one object of BankAccount class, which everybody works on
it. So TellerA, TellerB and TellerC all have a member reference to a
bankaccount and passing the object bankaccount is very deliberate and
redudant.
<snip example>
As you see, both TellerA and TellerB are working with the same account
object. But the object has to be passed deliberately. In procedural
language, you can leave the object in the global place and let everybody
access it, modify it, like a chain in a factory.
Indeed, but you do not need to create a global space, rather, you will
have a method, which knows about the bank account and the teller
machines. So it will create the bank account and pass it to the
tellers, something like so:
public someMethod() {
BankAccount theAccount = new BankAccount(1000000);
Teller tellA = new TellerA(theAccount);
Teller tellB = new TellerB(theAccount);
// have tellA and tellB do something with theAccount
}
So now someMethod is responsible for ensuring encapsulation, i.e. it
takes care of it that the bank account is not handed to anybody that has
no business with it. As you see, I made both Tellers inherit from an
abstract Teller class, as Javier already suggested.
Often, someMethod will be static and be called main...
H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)
iD8DBQFFF4sge+7xMGD3itQRAqjwAJ4l/hZ1B1h03hPMmHWtYwDJ3NmGdQCfdQPm
hHomUIHwF2dgQ8iYVe0biDo=
=rjtr
-----END PGP SIGNATURE-----