avatar
Design options# Java - 爪哇娇娃
g*y
1
- A DocumentCenter class processes XML document.
- A bunch of classes *Rule derived from BaseRule class, which will apply some
rule to XML document.
So, I need to pass DocumentCenter instance to Rule classes.
Two options I can see:
1. Set DocumentCenter as Singleton
2. In main class, pass DocumentCenter instance to each Rule class.
Considering future expansion/maintenance, which way would you go? or you have
better solution?
avatar
m*t
2

Being a Dependency Injection fan, I'd go with this one.

【在 g**********y 的大作中提到】
: - A DocumentCenter class processes XML document.
: - A bunch of classes *Rule derived from BaseRule class, which will apply some
: rule to XML document.
: So, I need to pass DocumentCenter instance to Rule classes.
: Two options I can see:
: 1. Set DocumentCenter as Singleton
: 2. In main class, pass DocumentCenter instance to each Rule class.
: Considering future expansion/maintenance, which way would you go? or you have
: better solution?

avatar
X*r
3
lol.. this is a good one :)

【在 m******t 的大作中提到】
:
: Being a Dependency Injection fan, I'd go with this one.

avatar
m*t
4

Self-mocking aside, the suggestion part was serious though...

【在 X****r 的大作中提到】
: lol.. this is a good one :)
avatar
w*g
5
option 1.
let BaseRule class call DocumentCenter to get an instance.

【在 g**********y 的大作中提到】
: - A DocumentCenter class processes XML document.
: - A bunch of classes *Rule derived from BaseRule class, which will apply some
: rule to XML document.
: So, I need to pass DocumentCenter instance to Rule classes.
: Two options I can see:
: 1. Set DocumentCenter as Singleton
: 2. In main class, pass DocumentCenter instance to each Rule class.
: Considering future expansion/maintenance, which way would you go? or you have
: better solution?

avatar
s*k
6
Create a main class, say PIPE. Available action PIPE would do is FLOW through
each VALVE attached to it.
DocumentCenter is the entry VALVE attached to the PIPE. Then other RULEs.

【在 g**********y 的大作中提到】
: - A DocumentCenter class processes XML document.
: - A bunch of classes *Rule derived from BaseRule class, which will apply some
: rule to XML document.
: So, I need to pass DocumentCenter instance to Rule classes.
: Two options I can see:
: 1. Set DocumentCenter as Singleton
: 2. In main class, pass DocumentCenter instance to each Rule class.
: Considering future expansion/maintenance, which way would you go? or you have
: better solution?

avatar
m*c
7
Or using "Decorator Pattern" on "Rule" class:
...
Rule rule = new Rule3 (new Rule2( new Rule3())); // Decorator Pattern
DocumentCenter.getInstance().setRule(rule); // Manual "Dependency Injecti
on" ???
DocumentCenter.process(xmlDoc);
...
avatar
g*y
8
Good suggestion, leads me to Martin Fowler's page -- www.martinfowler.com/arti
cles/injection.html
Have a good reading today.

【在 m******t 的大作中提到】
:
: Self-mocking aside, the suggestion part was serious though...

avatar
g*y
9
I don't fully get the idea -- my question is how to pass DocumentCenter instan
ce to Rule. Pass DocumentCenter/Rule as valve is OK to me, but in this design,
how does Rule get DocumentCenter instance?

【在 s*******k 的大作中提到】
: Create a main class, say PIPE. Available action PIPE would do is FLOW through
: each VALVE attached to it.
: DocumentCenter is the entry VALVE attached to the PIPE. Then other RULEs.

avatar
g*y
10
Yeah, Decorator fits here nicely. My framework is on similiar track, just litt
le difference --
Configure all Rules in XML file like:




...

then in main program, I extract our all rules with "run=1" and use reflective
to apply all Rules to document. (BaseRule has a method apply())
As compared to Decorator, coding would be
NodeDocument doc1 = NodeDocument(origDoc);
DefDocument doc2 = DefDocument(doc1);
GuidDocument

【在 m**c 的大作中提到】
: Or using "Decorator Pattern" on "Rule" class:
: ...
: Rule rule = new Rule3 (new Rule2( new Rule3())); // Decorator Pattern
: DocumentCenter.getInstance().setRule(rule); // Manual "Dependency Injecti
: on" ???
: DocumentCenter.process(xmlDoc);
: ...

avatar
s*k
11
Hehe, don't go for the idea of passing DocumentCenter instance to Rule.
I was thinking in the "other options" way, different entry point from ur initi
al design.

【在 g**********y 的大作中提到】
: Yeah, Decorator fits here nicely. My framework is on similiar track, just litt
: le difference --
: Configure all Rules in XML file like:
:
:
:
:
: ...
:

: then in main program, I extract our all rules with "run=1" and use reflective

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