So if really basic construction concepts like Delegation, Recursion and State Machine are "Design Patterns", shouldn't Creation/Allocation also be?
One object (call it the "master") needs another (call it the "slave") to complete an operation. The "master" needs a "new" object, initialized to a known state, and the master needs exclusive access so the "slave" doesn't undergo asynchronous state changes.
The master creates or allocates the object. There are a few current "Design Patterns" that all support this "master-creates-slave" functionality. They really shouldn't be patterns -- they are merely implementations of the same pattern. Those common patterns are:
- Creator/Constructor
- Factory
- Abstract Factory
- Object Pool
When modeling from a high enough level, these patterns all look the same. Whether or not an implementation uses a constructor, a factory or a pool is generally a performance choice. And the same "master-creates-slave" relationship can be implemented using any of them. In fact, you can easily refactor code that employs any one of these patterns to employ any one of the others pretty easily.
I would argue these four patterns are in fact allotropes of a single, unifying pattern called Allocation. Now I know the world doesn't need yet another pattern, but in this case I'm actually proposing we get rid of 4 and replace them with a single one.
Update: Don Box comments "Actually, I think if you add the 'Indirection' pattern, you could subsume the remaining 19 patterns from GoF :-)".
With "Allocation" I'm thinking about Ambler's Agile Modeling, and the practice of Applying Patterns Gently. (See later blog entry.) When in the course of modeling and design I have one object (or component) creating another, from a certain level of abstraction I don't really care how I'm creating the other object: using a factory, using a constructor, or allocating from a pool. Those decisions are lower level, and ideally the design should allow you to switch between them fairly agilely.
There is no adequately high level of abstraction that would allow you to collapse all the design patterns together and enjoy any process efficiency.
11:07:29 AM
|