Select Page

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.

Example Hammer hits a 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.

See also: Message (method-call)

Share This