Processing Advanced Intro #02 | Object-Oriented Programming

Contents

  1. Advantages of OOP
    1. Encapsulation
    2. Inheritance
  2. Practice
    1. Cluster of Random Walkers

Advantages of OOP

Encapsulation

Encapsulation is a packing of data and function into a single component. This is almost what we learned at previous session. Another feature of encapsulation is information hiding. Because one of the targets of OOP is to define object as concept model, some variables and functions don’t need to be handled from outside of the Class. For example, thinking about RandomWalker Class, perhaps we don’t need to access the variable of position. Also, encapsulation protects variables from unexpected modifications.

You can hide variables and function from outside of Class by using “private” when defining variables or functions.

class RandomWalker {
  //once we define these variables we don't need to care about them
  private int x;
  private int y;
  private int step;
  RandomWalker(int _x,int _y, int _step){
    x = _x;
    y = _y;
    step = _step;
  }

  void update(){
    move();
    display();
  }

  //these functions are needed only insude the class
  private void move(){
    PVector rand = PVector.random2D();
    rand.mult(step);
    x += rand.x;
    y += rand.y;
  }

  private void display(){
    strokeWeight(5);
    point(x,y);
  }
}

RandomWalker r;

void setup(){
  size(200,200);
  r = new RandomWalker(100,100,5);
}

void draw(){
  background(255);
  r.update();
}

Inheritance

Inheritance is a function to make new Class from existing Class with additional variables and functions. The advantage of inheritance is that we can reuse existing code by others to make our programs without reinventing existing code.

For example, you can make Agent(RandomWalker) Class as extension of PVector Class.

class agent extends PVector {
  private int step;
  agent(int _x, int _y, int _step) {
    //constructor of base Class
    super(_x, _y);
    step = _step;
  }
  
  void update(){
    move();
    display();
  }
  
  private void move() {
    PVector rand = PVector.random2D();
    rand.mult(step);
    //function of base Class
    super.add(rand);
  }

  private void display() {
    strokeWeight(5);
    point(super.x,super.y);//variables of base Class
  }
}

agent a;

void setup(){
  size(200,200);
  a = new agent(100,100,5);
}

void draw(){
  background(255);
  a.update();
}

Practice

Cluster of RandomWalker

class agent extends PVector {
  private int step;

  agent(int _x, int _y, int _step) {
    super(_x, _y);
    step = _step;
  }
  
  void update(){
    move();
    display();
  }

  private void move() {
    PVector rand = PVector.random2D();
    super.add(rand);
  }

  private void display() {
    strokeWeight(5);
    point(super.x, super.y);
  }
}

class cluster extends ArrayList<agent> {
  cluster() {
    super();
  }
  
  void add(int x, int y, int step){
    super.add(new agent(x,y,step));
  }

  void update() {
    for (int i=0; i<super.size (); i++) {
      super.get(i).update();
    }
  }
}

cluster c;

void setup() {
  size(200, 200);
  c = new cluster();
}

void draw() {
  background(255);
  c.update();
}

void mousePressed() {
  if (mouseButton == LEFT) {
    c.add(mouseX, mouseY, 5);
  }else{
    c.remove(0);
  }
}