当你应该封装的泛型类型? [英] When should you encapsulate generic types?

查看:172
本文介绍了当你应该封装的泛型类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过一些人建议你应该封装的泛型类型与类更接近你的域名,例如史蒂夫和NAT中的成长的面向对象的软件,通过测试了指导:

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-Oriented Software, Guided by Tests that:

我们的规则经验是我们试图限制类型的周围路过泛型[...]。特别是当应用到集合,我们认为它是重复的一种形式。这是一个暗示,有应提取到一个类型域的概念。

Our rule of thumb is that we try to limit passing around types with generics [...]. Particularly when applied to collections, we view it as a form of duplication. It’s a hint that there’s a domain concept that should be extracted into a type.

在一般情况下,当它是一个好主意这样的事情..

In general, when is it a good idea to do something like this ..

class PersonList : List<Person>

..而不是仅仅使用列表<人> 直接?

.. rather than just using List<Person> directly?

推荐答案

你所寻找的是一个Java 的typedef 运营商或C#。

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

不幸的是子类的方法是不适合的typedef 一个很好的替代品。

Unfortunately subclassing approach is not a good substitute for typedef.

下面的文章Java理论与实践:该伪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:

为伪typedef的反模式的动机很简单
不够 - 开发商希望的方式来定义更紧凑的类型
标识,尤其是仿制药制造类型标识符更
冗长。问题是,这种习惯用法创建采用它
码和该代码的客户,抑制再利用之间的紧密耦合。你
可能不喜欢泛型类型标识符的冗长,但这是
不是解决问​​题的方法。

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.

这篇关于当你应该封装的泛型类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆