General Question about java parameters / OOP good design techniques

A

antoine

Hello,

I'm curious about the way I'm coding, as I actually believe I'm not
doing things correctly. I have a few general questions and even after
having read a few books and browsed the internet for some time, I can't
find the correct document describing the methodology I'm after...

I'm going to illustrate my questions through an example

I am building an application that computes prices of different
financial instruments. those instruments can be grouped in "groups"
with common "characteristics".

let's call my main class MAIN.
In it, I instanciate 2 objects SubLevel, that I call subLevel_1 and
subLevel_2
for each of those SubLevel Objects, I instanciate 10 Instruments
Objects (I put them in a Vector).

1. first question: the instruments need to "know" things (variable
values, Objects AND ints, doubles, etc...) that are described in their
SubLevel parent, and also in their MAIN parent. What is the correct
(best / cleanest) way to access those variables (parameters) ?

- should I pass all variables as parameters of my Constructor method ?
- should I pass a reference to the parent object, and then access those
values through accessor methods only ?
- when the variable I want to access is in the MAIN, should I consider
accessing it through something like:
int value = subLevelParent.getMAINParent().getMyValue(); ??

2. something else: if I pass variables in the constructor method,
should I pass the variable itself (int or double, etc...) or its Object
version (new Double(double)) ?

3. finally: I perform computation with a pretty long (lots of
parameters), but simple formula. I have a class that holds this
formula, but what is the best practice to call it ?
- should I create several instances of the class (each of which sharing
common specific values - one per "SubLevel" instance for example) and
access each instance when I want to compute ?
- or should I simply create a static class with a method taking this
long list of parameters and always use this class without instancing it
?

hmmm... I get the feeling that I should add a "WHEN..." in front of
those questions as there might be several answers possible depending on
the case...

it looks like newbie questions to me, but that's only today after
having developped in java for 4 years (alone...) that I feel the need
to solve that, so I'd be happy to get any insight, answer, book or
webpage recommendation for this :)

thanks for your valuable help...

Antoine
 
S

sujee

It is better if u can study about 'OOP Design pattern' then use one of
the UML object model software(Rational Rose 2003). But for the time
being u can study basic object modeling of UML and use Rational rose.
(You can generate java code from RR) and go through the generated code
and clarify your doubts.

Q1)
Main class
SubLevel sl[];

SubLevel class
SubLevel(Main a,----------){} //cons
Instruments ins[];

Instrument class
Instrument(Main a,Sublevel sl,-----------){} //cons
Sublevel a;
instrument b;

I think u will instanciate the instrument object from SubLevel and
instanciate the SubLevel class from Main.
So add when instanciate send 'this' object. Before the create the
constructure to initiate the variable propely.

pass a reference to the parent object, and then access those
values

accessing it through something like:
int value = subLevelParent.getMAINParent().getMyValue();


Q3)
It is depend on your program if ur computation have more values related
to it and u need to use those values in future usage, create a class
and instanciate objects and use it.

otherwise use static methods.


If the calculation is related to ur exiting class (Eg Instrument) add
methods to that class. Don`t create separate class.
 
R

Rhino

I think u will instanciate the instrument object from SubLevel and
instanciate the SubLevel class from Main.

Please, for the sake of all of us who appreciate the English language not
being butchered, remember that there is no such word as 'instanciate': the
correct spelling is 'instantiate' ;-)

Rhino
 
R

Rhino

let's call my main class MAIN.
In it, I instanciate 2 objects SubLevel, that I call subLevel_1 and
subLevel_2
for each of those SubLevel Objects, I instanciate 10 Instruments
Objects (I put them in a Vector).

Please, for the sake of all of us who appreciate the English language not
being butchered, remember that there is no such word as 'instanciate': the
correct spelling is 'instantiate' ;-)

Rhino
 
J

John C. Bollinger

antoine said:
Hello,

I'm curious about the way I'm coding, as I actually believe I'm not
doing things correctly. I have a few general questions and even after
having read a few books and browsed the internet for some time, I can't
find the correct document describing the methodology I'm after...

I'm going to illustrate my questions through an example

I am building an application that computes prices of different
financial instruments. those instruments can be grouped in "groups"
with common "characteristics".

let's call my main class MAIN.
In it, I instanciate 2 objects SubLevel, that I call subLevel_1 and
subLevel_2
for each of those SubLevel Objects, I instanciate 10 Instruments
Objects (I put them in a Vector).

1. first question: the instruments need to "know" things (variable
values, Objects AND ints, doubles, etc...) that are described in their
SubLevel parent, and also in their MAIN parent. What is the correct
(best / cleanest) way to access those variables (parameters) ?

- should I pass all variables as parameters of my Constructor method ?
- should I pass a reference to the parent object, and then access those
values through accessor methods only ?
- when the variable I want to access is in the MAIN, should I consider
accessing it through something like:
int value = subLevelParent.getMAINParent().getMyValue(); ??

There is a semantic difference between feeding the values to the
instruments' constructors and having the instruments get the values at
need from other objects. If what the instruments really want is "the
values at the time I was constructed" then pass them to the constructor.
If they want "the value in my SubLevel (or in MAIN)" then have them
read those values from the relevant object.
2. something else: if I pass variables in the constructor method,
should I pass the variable itself (int or double, etc...) or its Object
version (new Double(double)) ?

There is no advantage to passing a corresponding wrapper object when
passing a primitive value is a viable alternative.
3. finally: I perform computation with a pretty long (lots of
parameters), but simple formula. I have a class that holds this
formula, but what is the best practice to call it ?
- should I create several instances of the class (each of which sharing
common specific values - one per "SubLevel" instance for example) and
access each instance when I want to compute ?
- or should I simply create a static class with a method taking this
long list of parameters and always use this class without instancing it
?

If you use an instance method on some object then your program can be
adjusted to perform a different computation simply by feeding it
different objects. This kind of design feature is an implementation of
the "Strategy" design pattern, and it is probably the better choice.
Whether you need or want multiple instances of the strategy class is a
separate question altogether. If instances are thread-safe and
indistinguishable then there is no need to have more than one, but you
can have as many as makes sense.
 
D

David

antoine said:
Hello,

I'm curious about the way I'm coding, as I actually believe I'm not
doing things correctly. I have a few general questions and even after
having read a few books and browsed the internet for some time, I can't
find the correct document describing the methodology I'm after...

I'm in about the same place as you when it comes to figuring this stuff out,
so please take my (following) comments and suggestions with a 'grain of
salt'.......
I'm going to illustrate my questions through an example

I am building an application that computes prices of different
financial instruments. those instruments can be grouped in "groups"
with common "characteristics".

let's call my main class MAIN.
In it, I instanciate 2 objects SubLevel, that I call subLevel_1 and
subLevel_2
for each of those SubLevel Objects, I instanciate 10 Instruments
Objects (I put them in a Vector).

1. first question: the instruments need to "know" things (variable
values, Objects AND ints, doubles, etc...) that are described in their
SubLevel parent, and also in their MAIN parent. What is the correct
(best / cleanest) way to access those variables (parameters) ?

- should I pass all variables as parameters of my Constructor method ?
- should I pass a reference to the parent object, and then access those
values through accessor methods only ?
- when the variable I want to access is in the MAIN, should I consider
accessing it through something like:
int value = subLevelParent.getMAINParent().getMyValue(); ??

I've been led to believe the above statement (and the like) is the result of
a poor design to begin with. For one thing, when the lower level objects
(servers) access the parent levels (clients) freely, then your code is
simply 'procedural' with a bunch of extra 'brackets' and stuff......
The Instrument level classes would need to know the 'signatures' of the
upper level class methods and variables, defeating the whole purpose.
It's kinda like the speakers of a 'sound system' being given the
responsibility to know (or adjust) the volume settings of an 'amp' before
they can function.
In my way of thinking, the MAIN should supply the SubLevel objects with
whatever they need to do their 'service', and the SubLevel object should
be able to supply the Instrument objects with what is needed.
The Instrument objects should basically be stand alone, and in effect,
could be moved from one application to another, and still function (with
their 'interface' supplied of course), and the SubLevel objects as
oblivious to their position 'in the whole' as well.
If they are not, then it's back to the drawing board, and try to figure out
why, and adjust the class boundaries and concepts so that it works that
way. It's a matter of the degree of OO achieved (or required) I think......
(I assume this is not an inheritance problem...)
I can usually get code to that point (a lot of my projects are still on the
shelf because of it though...), and just about every 'callback' inter-action
needed can be reduced to broadcasting 'events' from lower level
(server) objects to registered 'client' listeners.
The main critique I have encountered is that 'in the real world', trying
to do things OOP is just a waste of time, and a 'good coder' gets
things done in a 'jiffy' without worrying about all that crap.......
I dunno.......???

David Otte
 
S

sujee

Hi Rhino,
I am not expert in english language. But remember one thing language is
used to communicate. Not to check the spelling and grammer mistake. My
mistake is not that much. Bec u have found the meaning of that wrong
word. that is enough. But that is a careless mistake. Thank you for
your suggestion.

When we use the SMS who write the whole word or correct word? But the
receiver understand the meaning.
I will try to send as u expect.
 
R

Rhino

sujee said:
Hi Rhino,
I am not expert in english language. But remember one thing language is
used to communicate. Not to check the spelling and grammer mistake. My
mistake is not that much. Bec u have found the meaning of that wrong
word. that is enough. But that is a careless mistake. Thank you for
your suggestion.

When we use the SMS who write the whole word or correct word? But the
receiver understand the meaning.
I will try to send as u expect.
Sorry, I didn't mean to sound nasty. I hate seeing bad spelling in my native
language - maybe you feel the same way in your native language? - and I was
disappointed to see the word 'instantiate' spelled incorrectly in the
original post, then also being spelled the same wrong way in your reply. It
seemed to be spreading like a virus ;-) I just wanted you both to know the
correct spelling in case you didn't know and wanted to spell it correctly.

Rhino
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top