struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Erik Weber <erikwe...@mindspring.com>
Subject Re: Why Template Method instead of Strategy in Commons Chain?
Date Mon, 07 Mar 2005 01:04:04 GMT
I guess here's a way I can restate my question.

I'm with you on choosing composition rather than inheritance when it's 
possible and sensible. But it's not always obvious where and how 
inheritance can be replaced by composition. I find it to be a 
challenging part of programming. So that's why I became interested. 
Also, sometimes in prototyping, you design a good API, you use 
inheritance underneath to get something working, and then you hope to 
refactor later.

If (a literal) Template is this:

public abstract class Chain {

  public final void execute() {
    doA();
    doB();
    doC();
  }

  public abstract void doA();
  public abstract void doB();
  public abstract void doC();

}


Then I'm having trouble seeing where this exists in Commons Chain 
(sorry, I haven't exactly gone through a lot of code). What I do see is 
a sort of "higher" template -- A chain will be executed but the commands 
are virtual, as in a literal Template. Perhaps the answer is in your 
words when you say "implementations" of Commons Chain (rather than the 
libraries themselves). If that's true I would appreciate it if you could 
point out a clear example.

I think Bill's article overall is good because it explains pretty 
clearly how Commons Chain works, however, why does he bring up Template 
in the first place? That's where I get lost . . . Is he saying that 
Commons Chain offers a better way to solve the problem that a literal 
Template solves by using a sort of higher abstraction of a template (one 
that leaves class inheritance behind)? If that's true then it's possible 
that the detriments of Template no longer apply, and that Strategy might 
not be any better (or even as good). If that's not true, however,  and 
the code I posted above exists (almost) literally in Commons Chain (or 
in typical implementations), then it's a healthy question (as always) to 
ask, why not composition rather than inheritance, if possible?

I guess I need to spend more time with Commons Chain and maybe it will 
become clearer.

Erik





Dakota Jack wrote:

>I think I stated the problem badly, Eric.  If you look at the
>implementations of commons-chain you will see the implementations tend
>to use Template Method.  The chain is used to solve the rather
>troublesome problems with that pattern, and yet the people who go to
>all the trouble to change to chain do so while staying with the
>Template Method.  The move to chain, in my opinion, allows us to get
>free of the Template Method and to use Strategy instead.  Bill's
>article is about the use of Template Method with chain and the propsed
>changes in Struts v1.3 use Template Method with chain.  Chain solves
>many of the problems with Template Method, but my suggestion is that,
>if you are going to implement chain and make that big change in v1.3
>anyway, why not switch to Strategy will doing so and abandon all those
>problems.  (I am, perhaps, overstating the problems with Template
>Method a bit,, but this seems to be okay given that there is a better
>option available with Strategy.  I think people generally agree that
>Strategy is a better pattern that Template Method to do the same
>thing.)
>
>
>On Sun, 06 Mar 2005 18:22:44 -0500, Erik Weber <erikweber@mindspring.com> wrote:
>  
>
>>I think it's probably usually good to question Template when the
>>realization of it comes via class inheritance.
>>
>>However, I'm confused. I read Bill's article (first pass anyway -- and
>>I'm new to Commons Chain), and I'm still not seeing where exactly
>>Template is implemented in Commons Chain. He introduces Template and
>>then seems to say that while Template is still used, its detriments are
>>mitigated. But I need help understanding exactly where it is used in the
>>first place. (I'm not making the connection between the first code chunk
>>and the following ones.) Only then will I be able to assess the question
>>of whether Strategy would be better (or would even solve the same problem).
>>
>>Here's a diagram of Template:
>>
>>http://www.dofactory.com/Patterns/PatternTemplate.aspx
>>
>>And here's a diagram of Strategy:
>>
>>http://www.dofactory.com/Patterns/PatternStrategy.aspx
>>
>>Could you plug the Commons Chain classes (existing or proposed) into an
>>illustration (verbal will do) for me so I can better understand the debate?
>>
>>Thanks,
>>Erik
>>
>>
>>Dakota Jack wrote:
>>
>>    
>>
>>>I inquired why the Template Method pattern is being used with Commons
>>>Chain instead of Strategy, but never got an answer.  Given that the
>>>choice seems so problematic, that surprised me.  Is that (no response)
>>>because there is no answer, or because it is a closed shop of ideas in
>>>open source, or because there is no time to consider such a stupid
>>>idea, etc.?
>>>
>>>
>>>
>>>
>>>      
>>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>>For additional commands, e-mail: user-help@struts.apache.org
>>
>>
>>    
>>
>
>
>  
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Mime
View raw message