I started self learning design patterns from Design Patterns by Gang of Four
Parameterized types give us a third way (in addition to class inheritance and object composition) to compose behavior in object-oriented systems. Many designs can be implemented using any of these three techniques. To parameterize a sorting routine by the operation it uses to compare elements, we could make the comparison
- an operation implemented by subclasses (an application of Template Method (325)),
- the responsibility of an object that's passed to the sorting routine (Strategy (315)), or
- an argument of a C++ template or Ada generic that specifies the name of the function to call to compare the elements.
I understand generic concepts, but I was still wondering how the third way "make the comparison an argument of a C++ template or Ada generic that specifies the name of the function to call to compare the elements" is done?
The function to call to compare the elements is not a type, how can it be used as a type argument to instantiate a generic/template?
I'd appreciate some example(s) in whichever OO language: C++, C#, Java, Python, ...
Thanks.
Aucun commentaire:
Enregistrer un commentaire