Motivation:

I had an existing class hierarchy, where I sometimes wanted to make the same modification to two different classes, while leaving the original classes untouched. I considered the decorator pattern because it let me add functionality to objects of different types by intercepting then proxying method calls. My client using the decorated object saw these objects behaving polymorphicly because the method interface was constant, while object behavior varies by object. However, the decorator did not give me true polymorphism because the decorator class is not in the inheritence tree of the object it decorates. Therefore if code inside the decorated object calls one of it’s own member functions it will never execute the matching method of the decorator. What if you wanted true inheritance, but also wanted to add the same behavior to multiple types of objects? Lets take an example…

Lets say we have a Person class. From that class we derive both a Man and a Woman class.

PERSON | ------------------------------ | | MAN WOMAN

What if I introduce the concept of an insecure person? Lets say that insecurity manifests itself the same way in both sexes. I want to be able create a regular man, a regular woman, an insecure man or an insecure woman.

Adding another stipulation, lets say that insecurity modifies the existing methods of a person in some way, as opposed to just adding new methods.

I don’t want multiple inheritance since I am modifying existing methods of the class, not adding new methods that the class does not have. I also don’t want a decorator, as we will see later, because there are non-public methods called internally that I want overridden as well. I don’t want to put IF statements in the code to check if we are insecure, since I think inheritance is more maintainable. I also don’t want to create insecure man and insecure woman classes separately because they would share exactly the same code.

What I really want…

PERSON | ------------------------------ | | MAN WOMAN | |

INSECURE INSECURE

In programming languages I’ve used, it is not possible to create the class hierarchy above at design time. The “insecure” class has two different parents, but only one parent at a time.

What if we could separate the definition of the class hierarchy from the class definition itself? e.g. class InsecureMan = Person -> Man -> Insecure

class InsecureWoman = Person -> Woman -> Insecure

In python, you can approximate such a language construct because you can create and manipulate classes on the fly. It is likely that someone better versed in metaclasses could do a cleaner job than I have done, but I include here a version that has been working for me.

Lets say I start with Insecure derived from Man. It is not safe to just take an instance of the Insecure class and change it’s parent class from Man to Woman if you desire an Insecure Woman instance. If you had existing instances of Insecure Men you will have transgendered them by changing the parent of the Insecure class, since they all share the same class definition. Therefore, before linking classes together to build a class heirarchy we create a class shell whose parent we can set, then copy over references to all the class members and methods.

Alternative Designs:

There are ways via composition to solve this particular problem. You could provide the person with a VoiceBox, or an InsecureVoiceBox derived from a VoiceBox. The InsecureVoiceBox adds in the “if that is OK.” when asked to speak. But, if there are a wide variety of methods that need modification, or if the voice box needed access to Person methods composition might not fit as well.