java - Design Patterns: Callback as a method parameter -


I was thinking that defining callback for operation in a method parameter defines in the object and the setters, design patterns in the subject.

I'm not sure there is a design pattern about callback creation. For example, suppose there is some class A, and I want to execute a method M with some callb.

  Public category A {public interface callback {void onEvent (); } Public static method M (...) {// ...}}  

I can do this:

  public static zero method M (Callback C) {C.onEvent (); } // ... A.method (this); // The caller class is the callback! Instead of  

:

  public static zero set callback (callback callback) {this.callback = callback; } Public static zero method M () {this.callback.onEvent (); } // ... asset callback (this); // The caller class is the callback! A ritual ();  

Keep in mind that the fact of the method is stable, it is easy to understand just the scenario.

So, can I use the first approach as a legitimate design?

The reason for using the first scenario is to simplify the definition of easy callback for many execution to avoid memory leaks, because I control the list of callbacks using lists, supervisors etc. Will happen.

I do not think there is a "callback pattern", it is very common.

However, many design patterns use callback, such as

it will depend on the global structure of your program

that part of the code

  public static zero method M (callback c) {c.onEvent (); } // ... A.method (this); // The caller class is the callback!  

I do not bother, there is nothing like this presented in this way


Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -