java - Implementing a class that calls interface methods? -


i'm searching answer this. want create generic piece of code walks directory tree. maybe it's static class contains 1 method. no problem that. each file not directory, something. thought defining interface something thing (i call fileactioninterface.performaction() now), i'm not sure how code directory walker code call interface method (which doesn't exist yet). i've seen , used other java classes have method says "add interface handler()" class run implemented interface. if drop nothing in, nothing happens.

sorry grasping right words here. how code directory walker code call abstract code?

ps of these answers , i've marked them such. how when class has "addinterfacehandler()" method class? i'm guessing class has it's own implementation of interface, it's empty possibly. when call add() method either replace class' implementation or add array of interface implementations.

i'll start trying out different solutions.

you pass in object implementing said interface.

the walking method call method on object.

similar how files.walkfiletree works filevisitor.

very roughly:

public class walker {     private visitor v;     public walker(visitor v) { this.v = v; }     public walk(file dir) {         // pseudo-code         (file f : dir.getfiles()) {             if (f.isdir()) {                 walk(f);             } else {                 v.performaction(f);             }         }     } } 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -