While styding the Decorator Design Pattern i read there was a drawback on implementing the Decorator pattern using the module + extend + super implementation.
The drawbacks of this implementation are
can not use the same decorator more than once on the same object
difficult to tell which decorator added the functionality
Thinking about that i made the maesters example.
A maester is kind of scholars in the universe of Game of Thrones. They get rings of different metals on a chain the hang
around their necks symbolizing their mastery. The maester.rb
examples is a work around those drawback.
It works like this. The maester starts without any knowledge, any ring in his chain. Every time the maester study(extend)
a Tome
he increases his knowledge about the subject and get the correspondent ring on his chain. If he study more that
subject he will increase his knowledge but he already got the ring so his rings will remain unchanged.
To implement that behavior using the chosen design pattern the Tome
module defines the hook self.included
to add
another hook on the classes that include it. This method is the self.extended
method. This second hook sets
increases the knowledge of the maester (base class of the extended method) about the tome that is being extended.