java - Design Patterns: Callback as a method parameter -
i wondering if defining callback operation in method parameter worse defining in object , setting via setters, concerning design patterns.
i'm not sure if exists design pattern regarding callback creations.
for example, let's there class a, , want execute methodm callback.
public class { public interface callback { void onevent(); } public static methodm(...) { // ... } }
may this:
public static void methodm(callback c) { c.onevent(); } // ... a.method(this); // class calls method callback!
instead of:
public static void setcallback(callback callback) { this.callback = callback; } public static void methodm() { this.callback.onevent(); } // ... a.setcallback(this); // class calls method callback! a.method();
note fact of method static easy scenario understanding.
so, can utilize first approach valid design?
the reason using first scenario avoid memory leaks easy definition of simple callbacks multiple execution, since have command list of callbacks using either lists, observers, etc.
i don't think there "callback pattern", it's generic.
yet, many design patterns utilize callback, observer pattern or visitor pattern
it depends of global architecture of program
that piece of code
public static void methodm(callback c) { c.onevent(); } // ... a.method(this); // class calls method callback!
does not bother me, it's not forbidden if presented this
java design-patterns methods callback method-parameters
No comments:
Post a Comment