Sponsored Link •
|
Summary
A technique which I use over and over again in C++ and Heron, which also can be used in Java, is inheriting from a template parameter. I am searching for a name for this pattern. One of the trouble spots for this pattern is lack of constructor inheritance in C++ and Java, but this can be worked around.
Advertisement
|
Okay I confess, I never finished the GoF ( short for the book by the Gang of Four - Design Patterns: Elements of Reusable Object-Oriented Software by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides ). I read it at a time that I was doing more writing of code than writing about code, as I am doing recently. I found that it restated techniques that I was already using, and giving them catchy names. Now I wish I got to the end, because I lack a good name for a pattern that I want to write about. I would like to call it wrapping, but apparently that is taken.
The pattern is very simple: inheriting from a template parameter. It is gaining in popularity, and I see it popping up more and more often in other people's code. One way I use the pattern is to define contract classes, which verify the contractual obligations of another class ( plug: as described in my article on Programming with Contracts in C++ to appear in a future edition of Doctor Dobbs Journal ), Another way I use the pattern, is to define extension classes which introduce operators and helper functions which can be defined in terms of a core set of Some issues arise in C++ and Java due to constructors not being inherited, but in C++ at least -- and probably Java, though I don't know the syntax -- this can be worked around by parameterizing the constructor. This problem though doesn't occur in Heron because Heron classes automatically inherit constructors. To work around the problem in C++ I currently use a parameterized constructors:class Base { public: Base(int x) { printf("initializing constructor with int\n"); } Base(char x) { printf("initializing constructor with char\n"); } }; templateI am sure that this approach can be easily adopted to Java. If anyone has the name of the pattern, or has any comments, please share them!class Wrapper : public Super_T { public: template Wrapper(const T& x) : Super_T(x) { } }; int main(int argc, char* argv[]) { Wrapper a(5); Wrapper b('a'); getchar(); };
Have an opinion? Readers have already posted 8 comments about this weblog entry. Why not add yours?
If you'd like to be notified whenever Christopher Diggins adds a new entry to his weblog, subscribe to his RSS feed.
Christopher Diggins is a software developer and freelance writer. Christopher loves programming, but is eternally frustrated by the shortcomings of modern programming languages. As would any reasonable person in his shoes, he decided to quit his day job to write his own ( www.heron-language.com ). Christopher is the co-author of the C++ Cookbook from O'Reilly. Christopher can be reached through his home page at www.cdiggins.com. |
Sponsored Links
|