Action Object Principle

An action on an object in the real world, becomes a service (method) of that object in an Object Oriented approach.

If one object acts on another, which object should contain the action in Object Oriented code?

In the real world, one continually observes an object acting on another. A Hammer hits a Nail. A Customer purchases a Product. An Ingredient is applied to a Bread Slice. Which object of the pair contains the action in object oriented code?

For example, given a Hammer hits a Nail, the action “hit” becomes a method of the Nail.

From an object’s perspective: “I do the things that are done to the actual object that I’m an abstraction of“.

Procedural code gets information, then uses it to make decisions. Object oriented code tells objects to to things themselves.

Alberto, yes draw becomes a method of Paper. A Paper knows how it can be drawn on, and many different things could then send a message to a Paper to do so. You would typically pass as arguments in the draw method, those things the Paper needs for drawing. An example might be a particular pen. If drawing on the paper was affected by some attribute of a person, then the person might be passed as an argument. Perhaps draw works differently for someone drunk than sober.

However if you want to track or record a person drawing on paper, then a different pattern of objects interact together to represent that. One would make explicit the draw action as a transaction object, linked to the person and the paper. I’ll write about that pattern shortly.