It's been quite a while since my last techfile entry, but I finally found the time to write another piece in my ongoing series about OOP :-). Today, I want to explain a bit more about factories, what they are and, more importantly, what good they are to 3D-coders.
Last time I briefly mentioned design patterns. For those of you unfamiliar with the term, Design Pattern are hot in the OOP community. Design patterns are solutions to regularly occurring problems. These solutions were not specifically developed by a person to solve these problems, but rather they emerged over time as being good solutions. One of the accepted "rules" for a design-solution to become a pattern is that it must have been applied independently in at least three software-packages.
Patterns are discovered rather than developed. You probably noticed at some time in your programming-career that you had used the same kind of construction to solve a certain problem a number of times in a program. That is similar to how patterns are discovered: Someone is looking over designs, and notices that different people have used identical solutions for a certain kind of problem. Writing this down and publishing a pattern prevents others from re-inventing the wheel.
But enough about patterns in general. Let's focus on a pattern I consider very useful: Abstract Factory.
An abstract factory allows you to create families of related objects, without specifying their concrete classes. Let me explain: Suppose you have a 3D-engine that represents models as sets of XYZ-points (As most engines do). All works well, but you suddenly decide you want to be able to choose between the ordinary XYZ-representation and an advanced parametric representation. This means a lot of your routines will change, for example the rotate routine. Let's say there are four classes that need to change certain routines for this new representation, while the other classes remain unchanged. The classes that change are CModel, CLight, CCamera and CRasterizer. The logical course of action is to create four derived classes: CParamModel, CParamLight etc. At a number of places in your code you create instances of these classes using new. How do you determine which class to instantiate at these places, especially if you want to have the choice between the two representations, or if you want to mix them? One way is to make a switch-case at every instantiation-place:
The big advantage is that you can leave existing code alone. Simply assign another factory to a class (1 assignment), and it will create instances of the correct class without any modification to its code. Keep in mind that this is a very simple example. The abstract factory becomes really interesting for applications with more than two variations (eg XYZ, parametric and spherical coordinate representations) and in applications where the factory can change at runtime (Mix XYZ and parametric representations).
For completeness, here is the UML notation diagram for this pattern:
That's it for now. I have no idea when my next update will be. I'm in the process of starting up a company, so I'm quite busy, as you can imagine.
Download The Sample Program: ManicMotion.exe