Friday, August 19, 2005

Object Inheritance

I've long been a fan of "Streamlined Object Modeling". It's a book I keep coming back to over and over. I was reading it recently and wondered why I never noticed the connection with prototyped delegation and what they call "object inheritance". The book is a collection of domain modeling patterns and several use "object inheritance". One pattern in particular expresses the relationship between an actor and a role. For example, let's look at some java code:
/**
* The common behavior of the actor and the role
*/
public interface PersonProfile {
public String getName();
public void sendEmailTo(EmailAddress another, String message);
}

/**
* The actor's behavior
*/
public interface Person extends PersonProfile {
public void setName(String aName);
public void setEmail(EmailAddress anAddress);
}

/**
* The role's behavior
*/
public interface Employee extends PersonProfile {
public BigDecimal getSalary();
}

The implementation is not important. I wanted to show example interfaces and to show the relationships. The point to object inheritance is that the Employee forwards the calls for PersonProfile on to a Person that it holds on to. This allows you to pass around the role or actor through the same interface. Now, you might be thinking that's a lot of forwarding going on. It is. Now, you could get fancy by using a Proxy and an InvocationHandler. In fact, I do something similiar in Smalltalk when I compile the forwarders on the fly. It's a great modeling technique and is useful in several contexts for expressing clarity in the model. Prototype programming languages make this modeling technique easy to implment (no duplication or coding gymnastics required) by the use of traits. Simply put, traits is a composition technique to combine collections of behavior with data to construct objects. To learn more about traits, read the paper, Organizing without Classes.

The good news is that it's simple to do this technique in Smalltalk by use of the Traits package. Ruby even has it through its Delegate framework. Once you start thinking in Traits, it's hard to go back. Have fun with this new technique and happy coding!

"Streamlined Object Modeling" has the best discussion of modeling and implementing business rules in object-oriented languages that I have ever seen as well. It's a great book all around that balances theory and implementation. It's filled to the brim with great advice that every business modeler should know.

No comments:

Amazon