Object Oriented Programming
This post is written as a blog post on the Digital Media Studio Project blog Non-Linear Narratives.
In this post I want to discuss some aspect of object oriented programming, and some philosophy behind the non-linear computer program in its current version.
The program is built in Processing, which utilizes the Java programming language, and is ideal to make sketches with its easy-to-create graphical user interface (GUI). Processing has a clear division between its two runtime methods which are crucial for its execution: setup runs once as the program is started, and draw runs perpetually during runtime. The advantage using processing lies in the flexibility the user interface, and the simple instructions which has to be done to get action onto the screen. The threshold to get a visual representation is much lower than e.g. Swing. Since Processing is built on Java, we can also benefit from the strong usability, the many packages developed and the basis of object oriented programming.
In the case of Java the magic words for object oriented programming are class, new and ‘.’ (punctuation). The class name is used in the beginning of a class, and is followed by a descriptive name where the first letter, by convention, should be in capital. Two curly brackets are placed to mark the beginning and the end of the class. The class does not need an explicit constructor, but if you want to send some attributes to the class when you initialize it you should make one. The ‘new’ keyword is used to express that a new object should be created – or instantiated – from the class (Thanks to Kebman pointing this out). Metaphorically can we perceive the class as a mold, and the objects as casts created from this, and which constructor defines the customization. This is followed by the name of the class and attributes encapsulated in parenthesizes. The ‘.’ is used to call upon a method, or to access or alter one of the attributes contained by the class. This is done by placing the punctuation mark in between the pointer to the class and the attribute or method you want to access, call or alter.
The object oriented programming has been changing the way the world is represented within the computer since it was first invented in Simula 67 by computer scientists Ole-Johan Dahl and Kristen Nygård in the 1960s. Today this method is deeply integrated in many computer languages such as C#, Java, Lisp and PHP. Its advantages lies in the possibility of modularisation, encapsulation, messaging and abstraction as well as a way of representing reality which can be traced back in the history of philosophy.
Well, let us move from the syntax and history to the more philosophical ideas behind this way of perceiving the world. Do you remember Plato, the old Greek philosopher. He operated with two different world of existence. In his metaphysics things did not only exist in their earthly being, but also in a world of ideas. Without discussing whether this is a good or a true way of experiencing the world let us just keep the idea in our head (Plato’s concept) as we move to a more language bound example, the difference between the ‘the actor’ and ‘an actor’, here the former is the decisive actor standing in front of you in the queue, or in the neighbouring area, while an actor could be any actor, however ‘an actor’ could not be ‘an area’. So there are some distinctive features which separates actor from area. Let us agree upon these as the attributes, functions and relationships held by the classes.
With this, hopefully a bit confusing in mind, let us move on to a visual representation of our program.
This is a Unified Modelling Language (UML) class diagram of the program so far. If you remember that we spoke about the setup and draw class in the Processing language, or the Actor and Area. Well here is everything we need to know (attributes) and do (methods), as well as the relationships between them. Let us have a closer look at the class Area. This class has three attributes: it knows its X and Y position in the grid, as well as which actors who are currently visiting. What the area can do is to sign an actor in and out, and, return a binary boolean result whether something is bound to happen in the respective tile. (The execution of the program is all orchestrated by the all mighty control class, and the draw method.) The interesting thing which could happen here is depending on whether the tile has more than one visitor, as you may know, if this is the case then three possible actions can take place (in the current version represented by three separate recordings): Two victim meet, two suspects meet or one suspect and victim meet.
The classes Victim and Suspect are in the current version just used to identify the different objects, but can be expanded to act differently. Since they share the common superclass actor, they are in this version acting the same way.
The classes we see here is the mould. It is the idea, to use Plato’s term. As the program starts up the setup method calls upon a creation method in the control class which creates objects from the classes. How many are dependent on global variables set in the program, but currently we are operating with 16 times 16 areas (256 in total), 6 suspects and 2 victims. If we take a closer look at the areas these are stored within an double array Area which is a great way of representing a map, and works very good with double loops to iterate through.
I hope this post was able to explain the concept of object oriented programming more than it caused confusion. The current version of the program code can be found here. Enjoy.
3 thoughts on “Object Oriented Programming”
The “new” keyword is related to the creation of a new instance of the class, i.e an object, and thus object creation is often referred to as “instantiation”.
Great explanation of class structure! Seems like fun to program games!
You are right. It’s corrected now. It was very fun to program a game. I’ve been doing some programming recently, but hope to do more when the thesis is handed in. How are you?
Fine, thanks! 🙂 Currently I’m teaching as normal and trying to prog some web-projects on the side. Yes, thesis above everything!