|
![](/i/fill.gif) |
There really *are* design patterns which might sound obvious to you
and me, but are not all that obvious to many programmers who are starting
to dig into OOP.
One example I like (and which name I rather ironically cannot remember
now) is the design pattern which you can use to create primitives and
unions of primitives, so that the union itself is a primitive (thus
allowing a union to contain other unions, containing other unions,
thus forming a whole hierarchy of primitives).
For example, let's assume that you have three primitives: Line, rectangle
and circle, and you also want to be able to make compound objects containing
any amount of those (so that you can, for example, transform all of them
as if they were one single primitive). Of course it would be nice if such
a compound object could contain other compound objects as well. Thus this
compound object should be a primitive itself.
Hence the inheritance hierarchy would be something like this:
Primitive
'--> Line
'--> Rectangle
'--> Circle
'--> Union
'--Contains: List of <Primitive>
In other words, if that was drawn as an UML diagram, there would be
a "contains" arrow from 'Union' back to 'Primitive', making the hierarchy
recursive.
This is a simple idea and might be obvious to some, but it certainly
might not obvious to the new OO programmer.
The book Design Patterns is full of tidbits like this, making it
potentially quite useful to someone.
--
- Warp
Post a reply to this message
|
![](/i/fill.gif) |