Note that there are some explanatory texts on larger screens.

plurals
  1. PO"Inject" interface implementions into a class in java
    primarykey
    data
    text
    <p>I'm wrting something that look like this (of course its a bit more complex than this sample):</p> <pre><code>public class DoOnAll { private List&lt;IActionPerformer&gt; actionPerformers; public DoOnAll(List&lt;IActionPerformer&gt; actionPerformers) { this.actionPerformers = actionPerformers; } public void callFromSomeWhere(String path) { File f = new File(path); List&lt;File&gt; list = Arrays.asList(f.listFiles()); for (File file : list) { for (IActionPerformer action : actionPerformers) { action.perform(file); } } } } </code></pre> <hr> <pre><code>public interface IActionPerformer { public void perform(File file); } </code></pre> <hr> <pre><code>public class SomePerformer implements IActionPerformer { public void perform(File file) { if (getFileType(file) = ".txt") { doSomething } } } </code></pre> <p>I have 2 questions:</p> <ol> <li><p>Should I move the condition in <code>SomePerformer</code> to another method, <code>boolean accept(File file)</code> for example, and also add this method declartion to the interface? If so, how would I "collect" all the accepted classes in <code>DoOnAll</code>? just go through the <code>actionPerformers</code> list and add all the accepeted to another list and then go through the list of accepted and <code>.perform</code> on them? Or is there another way usually used in the methodology?</p></li> <li><p>Which ways are there for injecting the <code>actionPerformrs</code> list into the class? I want to write independent implementations and define in a file, say xml file, which ones to inject into the list.</p></li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload