Redian新闻
>
A design for parameter passing
avatar
A design for parameter passing# Java - 爪哇娇娃
g*y
1
Parameter passing between classes always bother me a lot. Noramlly, you take
parameters at Main class, then spread it to controls, then to different
modules/classes. A typical flow is like:
Main -> A -> B -> ... -> Z
Headache part is: At the beginning of design, you think B need param1 and
param2; then Main pass both to A, next B. Later, you find B actually needs
param1 and param3. Now you need to change all parameter passing sequence of
Main -> A, and A -> B. If the chain gets longer, it will b
avatar
g*g
2
It's like using a global hashtable to store all variables, I don't think
it's a good design.
Class A only needs parameters it needs to know, there's no point to
make parameter it doesn't need available in its scope.

【在 g**********y 的大作中提到】
: Parameter passing between classes always bother me a lot. Noramlly, you take
: parameters at Main class, then spread it to controls, then to different
: modules/classes. A typical flow is like:
: Main -> A -> B -> ... -> Z
: Headache part is: At the beginning of design, you think B need param1 and
: param2; then Main pass both to A, next B. Later, you find B actually needs
: param1 and param3. Now you need to change all parameter passing sequence of
: Main -> A, and A -> B. If the chain gets longer, it will b

avatar
g*y
3
Then what's your way to address the parameter passing hassle? Assumption is
that you will change passing parameter(s) between classes in future.

【在 g*****g 的大作中提到】
: It's like using a global hashtable to store all variables, I don't think
: it's a good design.
: Class A only needs parameters it needs to know, there's no point to
: make parameter it doesn't need available in its scope.

avatar
g*g
4
I think when the parameters get complicated, consider to pack the params
in a bean class and pass the bean.

【在 g**********y 的大作中提到】
: Then what's your way to address the parameter passing hassle? Assumption is
: that you will change passing parameter(s) between classes in future.

avatar
c*t
5
I think that for your problem:
1. Break them down into categories and have an object that represent
each category (and thus put attributes in that category only).
Then, you pass categories around.
Rarely you would create new categories of data, so, adding new
parameter only affect the data structure of categories object per se,
not any signatures of function calls. Thus saving you the refactoring
trouble.
2. Depending whether or not you clone the category object often, as well

【在 g**********y 的大作中提到】
: Parameter passing between classes always bother me a lot. Noramlly, you take
: parameters at Main class, then spread it to controls, then to different
: modules/classes. A typical flow is like:
: Main -> A -> B -> ... -> Z
: Headache part is: At the beginning of design, you think B need param1 and
: param2; then Main pass both to A, next B. Later, you find B actually needs
: param1 and param3. Now you need to change all parameter passing sequence of
: Main -> A, and A -> B. If the chain gets longer, it will b

avatar
g*y
6
Thanks for suggestion.
Creating category of data is a thought, just as DAO. But my problem is: I like
iterative model, quick modeling, prototype, further design, then more coding,
... During iterations, I change parameter passing. Normally, I only pass
minimum data as needed for object. Since our work is heavy data manipulation,
if I use DAO, there will be way too many classes cluttered in my design.
I do not worry too much about synchronization. It is single-thread, sequential
processing. I alr

【在 c*****t 的大作中提到】
: I think that for your problem:
: 1. Break them down into categories and have an object that represent
: each category (and thus put attributes in that category only).
: Then, you pass categories around.
: Rarely you would create new categories of data, so, adding new
: parameter only affect the data structure of categories object per se,
: not any signatures of function calls. Thus saving you the refactoring
: trouble.
: 2. Depending whether or not you clone the category object often, as well

avatar
b*g
7
I think most of such problems should be avoided by careful design. If you have
this problem again and again then you should make some better design practice
.
btw when this does happen, I just override a constructor or method, wouldn't
take long.

【在 g**********y 的大作中提到】
: Then what's your way to address the parameter passing hassle? Assumption is
: that you will change passing parameter(s) between classes in future.

avatar
c*g
8
1) use a refactoring tool.
2) could you share a real life example? Most cases you can design
such a way to avoid passing many parameters between differenct
classes, which is not a sound oo desgin in general.

【在 g**********y 的大作中提到】
: Parameter passing between classes always bother me a lot. Noramlly, you take
: parameters at Main class, then spread it to controls, then to different
: modules/classes. A typical flow is like:
: Main -> A -> B -> ... -> Z
: Headache part is: At the beginning of design, you think B need param1 and
: param2; then Main pass both to A, next B. Later, you find B actually needs
: param1 and param3. Now you need to change all parameter passing sequence of
: Main -> A, and A -> B. If the chain gets longer, it will b

avatar
m*t
9

Why does Main have to find all the parameters for other classes?
If a parameter comes from command line, sure Main needs to process it,
but for everything else, such as configurations, each class/component
should deal with it itself.
If the API chain Main->A->B->C->... is so easy to be penetrated by
any parameter changes, I would first ask myself whether I have got
too many layers in the design, and maybe some of them are not doing
anything but passing through parameters.
parameters
what
be
aff

【在 g**********y 的大作中提到】
: Parameter passing between classes always bother me a lot. Noramlly, you take
: parameters at Main class, then spread it to controls, then to different
: modules/classes. A typical flow is like:
: Main -> A -> B -> ... -> Z
: Headache part is: At the beginning of design, you think B need param1 and
: param2; then Main pass both to A, next B. Later, you find B actually needs
: param1 and param3. Now you need to change all parameter passing sequence of
: Main -> A, and A -> B. If the chain gets longer, it will b

avatar
g*g
10
For example, in an applet, images are better loaded by applet at startup, so
that there's no delay when an animation needs it. Image is better passed from
applet to animation control when the control is created.
That being said, I don't see why parameters should be passed for many levels.

【在 m******t 的大作中提到】
:
: Why does Main have to find all the parameters for other classes?
: If a parameter comes from command line, sure Main needs to process it,
: but for everything else, such as configurations, each class/component
: should deal with it itself.
: If the API chain Main->A->B->C->... is so easy to be penetrated by
: any parameter changes, I would first ask myself whether I have got
: too many layers in the design, and maybe some of them are not doing
: anything but passing through parameters.
: parameters

avatar
m*t
11

from
The images are better loaded at startup doesn't mean they have to be loaded
by the applet class (or any name we might call the "main" class). The applet
class could let the image control participate
in the initialization - the concerns of how to load the images are of the
image
control's not the applet class'. In other words, the applet class only pays
attention to the general initialization sequence, but not necessarily every
detail of it.

【在 g*****g 的大作中提到】
: For example, in an applet, images are better loaded by applet at startup, so
: that there's no delay when an animation needs it. Image is better passed from
: applet to animation control when the control is created.
: That being said, I don't see why parameters should be passed for many levels.

avatar
g*g
12
If you initialize every control in the very beginning, sure, you can let the
control load the images, however, if the control is supposed to be created
dynamically, such as different animation under different condition.
It's not possible to let a non-exist control to load the image. Instead,
use an image warehouse in applet and pass images to control when they
are created seem to be more efficient for me.

【在 m******t 的大作中提到】
:
: from
: The images are better loaded at startup doesn't mean they have to be loaded
: by the applet class (or any name we might call the "main" class). The applet
: class could let the image control participate
: in the initialization - the concerns of how to load the images are of the
: image
: control's not the applet class'. In other words, the applet class only pays
: attention to the general initialization sequence, but not necessarily every
: detail of it.

avatar
m*t
13

That's perfectly fine. Create a separate "ImageWarehouse" to contain the image
management (including loading) logic then. Just don't mix it in the main
class
itself. That's all I'm saying in relevance to the OP.

【在 g*****g 的大作中提到】
: If you initialize every control in the very beginning, sure, you can let the
: control load the images, however, if the control is supposed to be created
: dynamically, such as different animation under different condition.
: It's not possible to let a non-exist control to load the image. Instead,
: use an image warehouse in applet and pass images to control when they
: are created seem to be more efficient for me.

avatar
g*y
14
This is the project I am doing now --
I need to convert XML files version 1 to version 2. Files are stored in tree-
structure like unix-directory, and the rules are:
* some file in version 1 will be split into multiple ones of version 2
* other files will be structural changes
* Almost every file has its own special rule, and I cannot extract a general
one
* If files in one sub-directory, they use same rule.
- And structure changes can be drastic, DONT even think about using XSLT!
- A typical XM

【在 c**g 的大作中提到】
: 1) use a refactoring tool.
: 2) could you share a real life example? Most cases you can design
: such a way to avoid passing many parameters between differenct
: classes, which is not a sound oo desgin in general.

avatar
m*t
15

I don't see anything terribly wrong in this. If the parameters are constantly
changing, maybe consider a hierarchy of parameter bean classes (as somebody
else suggested earlier), so that you don't have to keep changing method
signatures.
Another alternative would be to make the Rule classes themselves parameter
beans. The Converter would inject parameters into a Rule bean prior to calling
its conversion method, instead of passing them as parameters. If most XML
files are large files, conversio

【在 g**********y 的大作中提到】
: This is the project I am doing now --
: I need to convert XML files version 1 to version 2. Files are stored in tree-
: structure like unix-directory, and the rules are:
: * some file in version 1 will be split into multiple ones of version 2
: * other files will be structural changes
: * Almost every file has its own special rule, and I cannot extract a general
: one
: * If files in one sub-directory, they use same rule.
: - And structure changes can be drastic, DONT even think about using XSLT!
: - A typical XM

avatar
c*g
16
What are exactly these "parameters"? to me, it looks like you have
three separate parts:
1) a spider like class, which goes through a directory.
2) a convertor, which is in charge of reading/wrting xml file,
parse/syntactic analysis xml file.
3) rules.
As i can see, the only parameter necessary to be passed from 1) -> 2) -3)
is pathname/File reference.
Just curious, what are these 15-20M xml used for? I heard people
used xml to replace database, but have never had a chance to see
a real case. :-

【在 g**********y 的大作中提到】
: This is the project I am doing now --
: I need to convert XML files version 1 to version 2. Files are stored in tree-
: structure like unix-directory, and the rules are:
: * some file in version 1 will be split into multiple ones of version 2
: * other files will be structural changes
: * Almost every file has its own special rule, and I cannot extract a general
: one
: * If files in one sub-directory, they use same rule.
: - And structure changes can be drastic, DONT even think about using XSLT!
: - A typical XM

avatar
g*y
17
File/directory path are most parameters, but not all. Some XML nodes need to
be passed among these class -- our XML structure is so wild, the change can be
ANYTHING, ANYWAY :-) totally up to programmer's taste.
15-20M xml are used for defining business data structure, database connection
mapping, our software specific data, also a lot of user-defined javascripts,
etc. When programming in financial business, you will know that amount is not
uncommon. There are just way too many variations, busine

【在 c**g 的大作中提到】
: What are exactly these "parameters"? to me, it looks like you have
: three separate parts:
: 1) a spider like class, which goes through a directory.
: 2) a convertor, which is in charge of reading/wrting xml file,
: parse/syntactic analysis xml file.
: 3) rules.
: As i can see, the only parameter necessary to be passed from 1) -> 2) -3)
: is pathname/File reference.
: Just curious, what are these 15-20M xml used for? I heard people
: used xml to replace database, but have never had a chance to see

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。