views:

464

answers:

8

I've noticed that a big part of my code is built around callbacks. Is this considered a "design flaw"? Are there better design patterns that I should follow?

+2  A: 

Not at all. In fact, it's a buzzword-level hot design practice: "dependency injection".

chaos
Callbacks and dependency injection are not the same.
Chuck Conway
I mostly use them for dependency injection, so they're in much the same conceptual space to me. Not identical, though, no.
chaos
You are right, they are very similar. I'd never thought of DI as a form of callbacks until you mentioned it.
Chuck Conway
My 'Not at all' was most specifically an answer to 'Is this considered a "design flaw"?', for the record.
chaos
You could reword it: it is even becoming fashionable to install and override callbacks dynamically.
reinierpost
+6  A: 

I guess you could see the observer pattern as something that could be used akin to callbacks. Have you checked them out?

In the book Pragmatic Programmer, they mention the example of a person waiting to be boarded on a flight. Instead of that person asking the check-in desk constantly whether it's okay for her to go on board (polling), the check-in desk announces publicly to all those interested when the flight is ready.

A pseudocode for this example might look something like this:

class Clerk implements CheckInNotifyer {
  BunchOfObservers observers = new Bunch();

  public void addObserver(CheckInObserver observer) {
    observers.add(observer);
  }

  private void notifyListeners() {
    observers.all.notifyCheckIn(new CheckInEvent());
  }
}

class Passenger implements CheckInObserver {
  public void notifyCheckIn(CheckInEvent event) {
    event.getPlane().board();
  }
}

class WaitingArea {
  public init() {
    Passenger passenger = new Passenger();
    Clerk clerk = new Clerk();

    clerk.addObserver(passenger);
  }
}
Henrik Paul
Whether the observer pattern can be meaningfully termed a management structure for callbacks is left as an exercise for the interested student. :)
chaos
+3  A: 

Another option that works especially well when you have a number of procedures with callbacks going at once is to use WaitHandles.

Joel Coehoorn
+3  A: 

it's not bad, it's just the only way to do functional programming in non-functional languages.

Javier
+1  A: 

It depends. For Javascript, I prefer the observer pattern for anything that can actually be aware of a fired event - mainly because you can register and manage multiple observers. But for something like an AJAX call, wrapping it in a closure and including the callback method seems like the cleanest approach.

Rake36
+2  A: 

It's also known as event-driven programming.

reinierpost
A: 

In an object oriented language I find polymorphism a much 'cleaner' (no casting and no messy function type declarations) and a more object oriented method (deriving from an interface) to achieve the functionallity provided by callbacks.

Roger Nelson
Of course, that's the way I do it in Java.
Geo
A: 

Callbacks are just lighterweight and idiomatic small forms of the command pattern. Maybe your design would benefif from some explicit command interfaces.

Jordão