Saturday, January 17, 2009

An interface in java is a contract, but it's a rather shallow contract. All it guarantees is that certain methods with certain signatures will be available. What these methods do when you call them is not covered by the contract.

It would be possible to have a protocol that provides a richer contract. One that can guarantee behavior. To do this one would need to employ digital signatures. You'd have a signature that is applied to a certain method implementation, which is perhaps open source, but at least trusted to behave in a certain way. When you instantiate the implementing class, you pass a set of signatures which are checked against the various method implementations. The class will only instantiate if all the signatures match. Furthermore, this check can be made at compile-time.

Signatures could apply to methods of other interfaces as well, not just the one being instantiated. This would mean that if method m of interface i were called during the course of executing any method of the instance, the signature would be checked. This has a couple of drawbacks. 1) it has to be a run-time check, and 2) there's no guarantee the method would be called. The only guarantee is that IF the method is called it will behave in a certain way.

Guaranteeing behavior on a deep level gets to be more complicated. It gets to the heart of the problem of fulfilling human expectations, which is fraught with difficulties.

No comments: