Human above all, with pathos, weaknesses and grumpy at times. Speak for myself; think out loud. Direct, seemingly hard faced. Urged to fix things. Am fortunate.
How To Think Of... Macros
Imagine that you are a spy and have been assigned the task to infiltrate a robot factory to plant a tracking device which will allow you to place a bug in every robot
produced by the factory and record every action it makes. That bug can then be used to replicate all the actions the robot made on another identical robot.
The Robot is described as follows
There are a couple of things that need to be taken care off.
First is how can you tell when a robot alerts, logs or moves? In other words, how do you trace a method call? The simplest way is to use composition.
Notice how the Bug class must implement the Robot interface so that it can perform the same actions as the robot. (It also helps go unnoticed)
The second challenge is how do you “save” a method call when a method is not a first class citizen?
For that we need an interface to describe a method that executes on a Robot
The reason there is a need for an interface is that each implementation differs on which method it calls on the Robot yet we want to treat them all the same when executed on a robot.
Notice how the implementation of the RobotMethod also keeps the parameters needed to execute a specific method.
Finally use a simple List to keep track of all the methods executed in order simply by adding them to the list.
create a class that implements the same interface as your Robot
have a reference to the actual robot
delegate each method call to the robot
on every method call create a new instance from the class describing the method that was executed
add that instance to the list