1

I am searching Javadoc to identify an implementation of the "strategy pattern" inside Javadoc due to research reasons. I found 2 classes that actually inherited from the FilterInputStream class, the class BufferedInputStream and the DataInputStream. The inherited classes override the read() method of FilterInputStream class. Now according to "strategy pattern" I have to find a method from another class in Javadoc which in its body the read() method is called as well. Can anyone help me, please?.

P.S If you have any other implementation of strategy pattern inside Javadoc in your mind please tell me.

Thanks in advance

Kindle Q
  • 944
  • 2
  • 19
  • 28
pikk
  • 837
  • 5
  • 21
  • 38
  • I've read this a couple times but I'm not sure what you mean by "in javadoc." Are you looking for a use of the strategy pattern in the code of a program that _produces_ Javadoc? – Pops Mar 28 '11 at 19:37
  • no actually i m looking in java library to identify strategy pattern. But with javadoc is easier to identify the inheritance instead of just open the java src and start looking :) – pikk Mar 28 '11 at 19:40
  • I think @pikk means: in the Javadocs for the Java platform. – Fred Foo Mar 28 '11 at 19:49

2 Answers2

2

Input streams look more as an example of Decorator pattern rather than Strategy.

Better examples of strategy pattern are uses of ThreadFactory and RejectedExecutionHandler in ThreadPoolExecutor.

EDIT:

RejectedExecutionHandler is an interface of a strategy which determines how ThreadPoolExecutor handles rejection of tasks. There are several concrete implementations of such strategires (ThreadPoolExecutor.AbortPolicy, ThreadPoolExecutor.DiscardPolicy, etc). ThreadPoolExecutor can be configured to use one of them.

So, it corresponds to this picture (from wikipedia article) in the following way:

  • ThreadPoolExecutor is a Context
  • RejectedExecutionHandler is a Strategy interface
  • ThreadPoolExecutor.AbortPolicy, ThreadPoolExecutor.DiscardPolicy are concrete strategies (ConcreteStrategyA, ConcreteStrategyB)
axtavt
  • 239,438
  • 41
  • 511
  • 482
  • hm but ThreadFactory and RejectedExecutionHandler do not override any methods of ThreadPoolexecutor – pikk Mar 28 '11 at 20:15
0

I'd say that anything that uses Java's ServiceLoader scheme is using the strategy pattern. Basically the algorithms (maybe a huge suite of them) are undecided until runtime. Service Loader itself becomes a sort of master strategy (maybe that's off topic here) but anything that uses the ServiceLoader (e.g., CharsetDecoder) is following the strategy pattern approach.

Edit to add in response to comment: My understanding of "strategy pattern" is that it is a parent object into which a specific algorithm or algorithms may be decided upon and injected at execution time. So ServiceLoader itself is not a strategy pattern, but facilitates many areas of the JDK and other applications via SPI that employ the strategy pattern.

But maybe I'm making it too difficult. Basically, Collections.sort(List, Comparator) and any of the sorted collections with Comparator constructor arguments (e.g., new TreeSet(Comparator) are also examples. Why? Because, at execution time, any suitable comparator may be submitted to the sort() or constructor in order to change behavior. Normally in the Strategy pattern, there would be a number of implementations that could be chosen at execution time -- think for example of a table of emails that may be sorted by increasing/decreasing date, subject, or from-address order. Each of these would have an associated Comparator.

andersoj
  • 22,406
  • 7
  • 62
  • 73