Polymorphism is a big word for an important technique in object-oriented programming to help make code extendable. Rather than expressing business rules as conditional statements in code, we move them into factories and use them to instantiate objects that do the actions we want to take.
We need to know different things when we create an object versus when we use an object so separating these two activities can improve encapsulation and code cohesion. To create an object, you must know the object’s type. To use it you must know the signature of the methods you want to call, but you don’t have to know the object’s type, you could know the type of its parent or an interface that it implements. Polymorphism can give you the freedom to extend a type without breaking existing code.
This is also one of the key techniques in doing emergent design because it’s natural for a lot of good design to come out of refactoring when we build software in this way. We can come up with a better implementation for code that we already have under test so it’s far easier and more reliable to refactor than to write new code that’s not under test.
This is very much in line with my views on how to do test-first development where we write a test that fails and then try to make it succeed as quickly as possible, using the simplest implementation necessary, then work on refactoring it to make it more robust and maintainable. This is definitely a far easier and less error prone approach to writing code but few developers make use of it.
Of course, there’s a lot more to emergent design than just putting business rules in factories, although that is an important and not well understood aspect of good object-oriented programming, if not Agile development, in general. The reason for this is that the more differences we can handle as an issue of instantiation, the less we have to do with it in our production code, and that greatly simplifies the production code.
For example, a circle, a square, and a triangle are very different shapes and the way they’re drawn is completely different, but if I can handle those differences and draw them up on the screen as an issue of instantiation then I can treat them all the same way in my production code and move them or erase them or fill them with the same calls.
We refer to this as type encapsulation because we’re hiding the specific type that we’re working with from the one who’s using it. For example, the drawing program is unaware if it’s working with a circle or rectangle, all it needs to know is that it’s dealing with a shape and that a shape can be moved or resized. This not only simplifies the drawing code tremendously but it also makes the code far more general purpose because we can add new shapes and as long as they’re able to be moved and resized, they can interact with and be manipulated by the drawing program without having to change the drawing program at all. This type of extensibility is essential for emerging software.
The way object-oriented systems achieve this level of extensibility is through polymorphism. A polymorphic method is simply a method that shares its name with methods in other objects, which are typically sibling objects.
For example, a rectangle and a circle may have a polymorphic method called resize. The way we resize a rectangle versus a circle are different so each of the shapes will be responsible for resizing themselves in a different way that’s appropriate for them. But because each of them has a resize method, the drawing program can simply tell the shape to resize and not be involved in the specific implementation details of resizing because all shapes have the ability to resize.
A triangle is a shape, as is a circle and a rectangle. They are kinds of shapes because they share a common interface to polymorphic behavior. Circles and triangles and rectangles are represented in software as classes. There’s also the notion of an abstract class that represents a group of concrete classes. For example, a shape can be an abstract class. Abstract classes typically have some abstract methods that represent the method signature, or the way it’s called, but not what it does. The body of an abstract method is defined in the subclasses of the abstract class, for example a rectangle or a circle or a triangle.
Callers, like the drawing program, can only use concrete classes such as rectangles or circles or triangles, but they can hold those concrete classes in an abstract upcast type like shape and not have to know what specific derivation they’re using. This gives us a way of decoupling callers from the specific implementations they use. It’s one of the main ways that we “design to interfaces” in software development.
It helps to know the principles of good object-oriented programming. And it helps to know about design patterns and refactoring. All of these things together help us do emergent design efficiently. The most valuable part of doing emergent design is employing good techniques around breaking dependencies and encapsulating details. By mastering these techniques, we can be armed with tools that will help us in many situations, especially when we don’t know some of the requirements, or they’re likely to change in the future.
{2 Comments }
Previous Post: « Pair Programing Paradoxes
Next Post: How Small should a Story Be? »
Thank you David for this great post. Big help for clarifying the concepts. It brought me back to review the part of Factory in the Design Pattern section from the class.
You are welcome. I’m glad to know that you found value in my post! Best, David.