Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2006

Tip: Looking for answers? Try searching our database.

OO is not that great[2]: repeatedly passing the same object

Thread view: 
Shawn - 22 Sep 2006 19:22 GMT
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.

public class TellerA
{
    BankAccount myAccount = new BankAccount("1 million");

    ..// did something to myAccount

    TellerB b = new TellerB(myAccount, parameter A, parameter B); //I feel
the parameter myAccount is so redundant

}

public class TellerB
{
    private BankAccount ac = null;

    //constructor
    public TellerB(BankAccount ac, parameter a, parameter b)
    {
        this.ac = ac;
        ...// do something to the bank account -- same account object
        ...//then pass it to TellerC who keeps working on it
    }
}

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.
Michael D. Carney - 22 Sep 2006 19:35 GMT
... example deleted...

> 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.

You said it.... EVERYBODY can access it.... you have absolutely no
guarantee that another piece of code that you know nothing about
accesses and modifies your global object.

Arguments == good
Global data == bad.

- Mike.
Lasse Reichstein Nielsen - 22 Sep 2006 19:59 GMT
> 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.

You can leave the account in a global variable in Java too (it's just
called a public static field), and it has all the shortcommings of
global variables too.

Your design seems very far from an actual domain (does a TellerA
really *create* a TellerB?).
The roles are not clear, the object life times are untraditional,
there is no apparent reason why the tellar should be bound to
a single account, and there is little messaging going on between the
object instances, and all the computation happens in the constructors.
All in all, I'd say it's a case of bad object oriented analysis
and/or design.

/L
Signature

Lasse Reichstein Nielsen  -  lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
 'Faith without judgement merely degrades the spirit divine.'

Javier - 23 Sep 2006 01:18 GMT
Shawn ha escrito:

> 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.

TellerA, TellerB and TellerC are all Teller classes with things in
common (like BankAccount), so you should define a class named Teller,
with all the common coded embeded into it, and then inherit from it
TellerA, TellerB, and TellerC. There is not redundancy in this way, and
it is the way it should be if you pretend to correctly design your
program.
In this way, you can even add TellerD and other Tellers somwhere in the
future with very little effort.

Look at this:

http://java.sun.com/docs/books/tutorial/java/IandI/subclasses.html
Stefan Ram - 23 Sep 2006 01:26 GMT
>http://java.sun.com/docs/books/tutorial/java/IandI/subclasses.html
                                             ¯¯¯¯¯
 Looks as if Sun has hired Rastafaries recently.

     »"I and I" means that God is in all men.«

http://en.wikipedia.org/wiki/Iandi
Hendrik Maryns - 25 Sep 2006 08:54 GMT
Shawn schreef:
> Hi,
>
[quoted text clipped - 6 lines]
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.