When should you encapsulate generic types?

前端 未结 3 1800
南旧
南旧 2021-01-18 11:38

I\'ve seen a number of people suggest that you should encapsulate generic types with a class closer to your domain, for example Steve and Nat suggest in Growing Object-Orien

3条回答
  •  半阙折子戏
    2021-01-18 12:28

    What you are looking for is a typedef operator for Java or C#.

    Unfortunately subclassing approach is not a good substitute for typedef.

    The following article "Java theory and practice: The pseudo-typedef antipattern" explains why in full detail.

    I will copy verbatim the conclusion of that article here:

    The motivation for the pseudo-typedef antipattern is straightforward enough -- developers want a way to define more compact type identifiers, especially as generics make type identifiers more verbose. The problem is that this idiom creates tight coupling between code that employs it and that code's clients, inhibiting reuse. You may not like the verbosity of generic type identifiers, but this is not the way to solve it.

提交回复
热议问题