Quick and Accessible Inversion of Control

March 14, 2014 at 1:37 pm (Computer Science, Games, Graphics, Java, Processing, Programming, Rant, Teaching, Video games) (, , , , , )


So by now you’ve probably at least heard of the magical beast known as “Inversion of Control”. You may even understand what it is. If you’re in this boat, why aren’t you using it more often?? (if you are, you can read the rest of this article with your head held high).

There are tons of places on the interwebs where you can learn all about Inversion of Control (or IOC), so it doesn’t make sense to write up a detail explanation and then have people correct me on it. The short version of it is that it lets you defer your problems to someone else (future you for example). You allow the implementation to be abstracted in such a way that you can conveniently inject it in such a way that would make your college programming professor proud and/or confused.

For this example, I am going to use PicoContainer and Processing. PicoContainer is an IoC library for Java, although you could implement what it provides yourself through a variety of techniques I won’t tell you about here. The idea is just to show you a brief intro to what it can look like. In my example, I have a Graph object. It is the “M” of our MVC, and we want to separate the “V” from it. This means that the bit of code that draws the graph on the screen is going to be extracted. The kicker is that the way we abstract it will allow us to re-skin the graph in whatever way we want, whenever we want. We just inject a new view without changing the model layer and we’re good to go.

/**
  This is our interface for abstracting the View from the model
*/
public interface GraphView {
  public void draw(PGraphics g, Graph graph); 
}

/**
  This is our Model: The object we want to abstract the view from.
*/
public class Graph {
  private MutablePicoContainer graphConfig = null;

  public Graph() {
     graphConfig = new DefaultPicoContainer();
  }

  public MutablePicoContainer getGraphConfig() {
      return graphConfig; 
  }

  public void draw(PGraphics g) {
     // I'd recommend caching the view ahead of time to remove overhead
     GraphView graphView = (GraphView)graphConfig.getComponent(GraphView.class);
     graphView.draw(g, this);
  }
}

Now here is the part that makes it all work. I’ve written this in Processing code, but it is essentially simplified Java anyway. In this snippet of code (although it is a working program if you include the previous snippets), I instantiate our Graph object and then inject a View. In this example I’m just showing the power of IoC by including an anonymous implementation of a View object. In the “real world”, you would have written it as a standard Java object.

import org.picocontainer.*;

Graph graph = null;
void setup() {
  graph = new Graph();
  graph.getGraphConfig().addComponent(new GraphView(){
      public void draw(PGraphics g, Graph graph) {
        g.pushStyle();
        g.stroke(200);
        g.noFill();
        g.ellipse(25, 25, 10, 10);
        g.popStyle();
    }    
  });  
}
void draw() {
  background(0);
  graph.draw(g);
}

Now to summarize, we have accomplished our goal of abstracting the view of the graph from the data represented by the graph object itself. This means that we could completely re-skin the visual representation of our graph without touching any of our previously written code. If we wanted to draw the draw in 3D, we could just inject a new implementation. If we want it to be rendered using only ASCII characters, we got this. In the case that you still don’t see the value of this, consider a game you’re probably writing right now in your spare time:

You have a general “GameCharacter” class that both the PlayerCharacter and AiCharacter classes inherit from (Because you’re object-savy and generally good-looking). Both of these allow you to draw the characters differently. What happens when you decide to add new AiCharacters that act exactly the same, but look different? Well, you could throw in an IF statement and just check a state variable…. OR you could abstract the view, just like in our example! When you construct the AiCharacter objects, you inject a View object that will do all the drawing. As an object-oriented purist, you feel content, and as a game developer you feel pleased that you were able to trim off an “IF” statement from your render loop. You high five yourself.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: