If you have never heard of object-orientation, you should check sun's summary about object-oriented concepts first. We create a simple but fun simulator simulating the behaviour of a simplified ant swarm.

In an ant swarm there are ants. Lots of ants :) But it's not that simple, there are different types of ants: on the top of the hierarchy, there is the queen. Under her, there are workers and drones. The workers get food from the outside world, and fight ants from other swarms, the drones take the food to the larvas.

How should we model this? Basicly, there are swarms, ants, and food. Swarms are represented by the queens, so there is no need for a swarm instance. Only ants and food left. What kind of ants should we model? We need a queen, workers, and for a spectacular simulation, let's say that the drones are the fighters, only they can fight other ants, but they can't get food, and only the workers can get food, but they can't fight other ants.

Now we can start object-orienting. Our most important class will be the Ant class. What fileds and methods should it contain? What are the common attributes of an ant? Well, an ant surely has a position and health. We want to differ them by color, so we need a color field. We will program them as state-machines, so a state attribute is also needed. And finally, our ants have to interact with the outside world, so a world field is needed.

And what methods should we implement? Well, the only method what is common in the three types of ants will be "receiveHit", because all ants can be injured by other ants. And a step function also needed for the state machine. Here is the UML diagram of our Ant class:



But we need three different types of ants. Queen can be inherited from this class directly, only an additional food field is necessary for her, to store avaiable food mass. We have to override Ant's step function, the different types do different things. And a receiveFood method is also needed.



Drone and Worker comes, but we should think a little first. They are different, but they have a few common things what Queen doesn't have : they can move, they can search for food/enemy, they can reach a target. So, mid-level subclass would be a good idea, from which we can inherit these classes. Let's call this class MovingAnt. A MovingAnt can search, can reachTarget, and can move. Fields needed for this operations: turn, angle, speed, target.



Now we can inherit Drone from it. Drone will be the figther ant, it has an attack power, and an overridden step function.



Worker is also simple: it has a power field, which represents the food amount transported by ant, and a queen field, because we need to know which is and where is our queen. And an overridden step function.



Food will be the next, it is a very simple class, it has a position, power, and the outside world to interact with. And one method: cleanUp, if someone picked it up.


And only on main class left. The outside world. As we see two different classes will "use" it, or more, if we extend our code a little, and it can change rapidly during development, so we better define it as an interface. What methods should it contain? We will surely need an addQueen, addDrone, addWorker and addFood method to construct our scene. That's why a removeAnt and removeFood method will also be needed. And to help our ants exploring the world, a getVisibleFood and getvisibleEnemy methods are necessary.



And our model is ready. For our program two additional classes will be needed: the main class, which controls and starts the game, and also acts as World, so it must implement the IWorld interface, and a class with static properties to store game constants. Final UML diagram will look like this: