one class per file idiom for multiple source files

your processing sketch is fabulous, your line count is in the hundreds, you have split your sketch to multiple files, and now you probably have one or more of the following problems:

  1. you are running out of names because every name has to be unique across all files.
  2. you have trouble finding the definition of a function because you forgot in what file it was defined.
  3. you have lost count what variable is used where because every variable is global.

one solution is wrap every file in a class. preferably with the same name as the file.

(this is a followup to: https://forum.processing.org/two/discussion/23442/how-processing-handles-multipe-source-files)

demonstration:

catmousegame/catmousegame.pde

Game game;

void setup() {
  size(400, 400);
  game = new Game();
}

void draw() {
  background(0);
  game.frame();
  game.draw();
}

void keyPressed() {
}

catmousegame/game.pde

class Game {

  Cat cat;
  Mouse mouse;

  Game() {
    cat = new Cat();
    mouse = new Mouse();
  }

  boolean catonmouse() {
    return false;
  }

  void newmouse() {
  }

  void frame() {
    cat.move();
    mouse.move();
    if (catonmouse()) {
      cat.eatmouse(mouse);
      newmouse();
    }
  }

  void draw() {
    cat.draw();
    mouse.draw();
  }

}

catmousegame/cat.pde

class Cat {

  int catx;
  int caty;
  int mouseeaten;

  Cat() {
  }

  void move() {
  }

  void eatmouse(Mouse mouse) {
  }

  void draw() {
  }

}

catmousegame/mouse.pde

class Mouse {

  int mousex;
  int mousey;

  Mouse() {
  }

  void move() {
  }

  void draw() {
  }

}

the "compiled" java file:

import processing.core.*; 
// bunch of imports

public class catmousegame extends PApplet {

Game game;

public void setup() {

  game = new Game();
}

public void draw() {
  background(0);
  game.frame();
  game.draw();
}

public void keyPressed() {
}

class Cat {

  int catx;
  int caty;
  int mouseeaten;

  Cat() {
  }

  public void move() {
  }

  public void eatmouse(Mouse mouse) {
  }

  public void draw() {
  }

}

class Game {

  Cat cat;
  Mouse mouse;

  Game() {
    cat = new Cat();
    mouse = new Mouse();
  }

  public boolean catonmouse() {
    return false;
  }

  public void newmouse() {
  }

  public void frame() {
    cat.move();
    mouse.move();
    if (catonmouse()) {
      cat.eatmouse(mouse);
      newmouse();
    }
  }

  public void draw() {
    cat.draw();
    mouse.draw();
  }

}

class Mouse {

  int mousex;
  int mousey;

  Mouse() {
  }

  public void move() {
  }

  public void draw() {
  }

}

  public void settings() {  size(400, 400); }
  static public void main(String[] passedArgs) {
    String[] appletArgs = new String[] { "catmousegame" };
    if (passedArgs != null) {
      PApplet.main(concat(appletArgs, passedArgs));
    } else {
      PApplet.main(appletArgs);
    }
  }
}

things of note:

  • as before everything gets concatenated.
  • the main file can not be put in a class. the setup() and draw() functions must to be top level.
  • there is nothing forcing you to have the same name for class and file. the class name doesn't have to be CamelCase. but the idea is to keep your sanity, remember?
  • there is nothing stopping you from putting many classes in the same file. or not putting classes in a file. again, the idea is to keep your sanity. so if you can cope with that then go ahead and do it. if not then don't.

the advantage of classes:

  • namespace. the classes insulates the names from each other.
  • encapsulation. or instance variables.
  • instances. you can instantiate many objects of one class. for example more than one mice.
  • basically all of the advantages of object oriented programming.

the disadvantage:

more notes:

  • the methods and attributes (functions and variables) can be declared private or static. that has some advantages but usually you don't really need them. i have not really tried so i can't tell how they behave.
Sign In or Register to comment.