g**********y 发帖数: 14569 | 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? |
m******t 发帖数: 2416 | 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?
|
X****r 发帖数: 3557 | 3 lol.. this is a good one :)
【在 m******t 的大作中提到】 : : Being a Dependency Injection fan, I'd go with this one.
|
m******t 发帖数: 2416 | 4
Self-mocking aside, the suggestion part was serious though...
【在 X****r 的大作中提到】 : lol.. this is a good one :)
|
w*******g 发帖数: 9932 | 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?
|
s*******k 发帖数: 20 | 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?
|
m**c 发帖数: 90 | 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);
... |
g**********y 发帖数: 14569 | 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...
|
g**********y 发帖数: 14569 | 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.
|
g**********y 发帖数: 14569 | 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); : ...
|
s*******k 发帖数: 20 | 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
|