什么是一个标记接口的目的是什么? [英] What is the purpose of a marker interface?

查看:294
本文介绍了什么是一个标记接口的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是标记接口的目的是什么?

What is the purpose of a marker interface?

推荐答案

这是一个有点基于米奇小麦响应切线。

This is a bit of a tangent based on the response by "Mitch Wheat".

一般情况下,任何时候我看到人们举架构设计指南,我总是喜欢提的是:

Generally, anytime I see people cite the framework design guidelines, I always like to mention that:

您通常应该忽略的框架设计指南的大部分时间。

这是因为与框架设计指南的任何问题并非如此。我认为.NET框架是一个梦幻般的类库。了很多fantasticness从框架设计指南流程。

This isn't because of any issue with the framework design guidelines. I think the .NET framework is a fantastic class library. A lot of that fantasticness flows from the framework design guidelines.

不过,设计原则并不适用于大多数程序员编写的最code。他们的目的是使所使用的数百万开发者的大框架的建立,而不是使图书馆的写作更高效。

However, the design guidelines do not apply to most code written by most programmers. Their purpose is to enable the creation of a large framework that is used by millions of developers, not to make library writing more efficient.

很多在它的建议可以指导你做的事情是:

A lot of the suggestions in it can guide you to do things that:


  1. 可能无法实现的东西的最直接的方法

  2. 可能会导致额外的code复制

  3. 可能有额外的运行时开销

在.NET Framework大,真大。它是如此之大,它是绝对不合理的假设,任何人有详细的了解它的每一个方面的知识。事实上,它是更安全的假设,大多数程序员经常遇到,他们以前从未使用过该框架的部分。

The .net framework is big, really big. It's so big that it would be absolutely unreasonable to assume that anyone has detailed knowledge about every aspect of it. In fact, it's much safer to assume that most programmers frequently encounter portions of the framework they have never used before.

在这种情况下,一个API设计者的主要目标是:

In that case, the primary goals of an API designer are to:


  1. 让事情与框架的其余部分
  2. 一致
  3. 在API表面积消除不必要的复杂性

框架设计指南推开发者创建code,它完成这些目标。

这意味着做这样的事情,避免继承层次,即使这意味着复制code,或推动所有的异常抛出code走出去切入点,而不是使用共享佣工(使堆栈跟踪赚更多在调试程序意义上的),以及很多其他类似的东西。

That means doing things like avoiding layers of inheritance, even if it means duplicating code, or pushing all exception throwing code out to "entry points" rather than using shared helpers (so that stack traces make more sense in the debugger), and a lot of other similar things.

主要的原因,这些准则使用属性,而不是标记接口暗示是因为删除标记的接口使得类库的继承结构更加平易近人。提供30种和继承层次结构的6层的类图是非常艰巨的比较来与15种和2层的层次。

The primary reason that those guidelines suggest using attributes instead of marker interfaces is because removing the marker interfaces makes the inheritance structure of the class library much more approachable. A class diagram with 30 types and 6 layers of inheritance hierarchy is very daunting compared to one with 15 types and 2 layers of hierarchy.

如果真的有百万的开发者使用的API,或code群是真正的大(说超过10万LOC),那么以下这些准则可以有很大的帮助。

如果500万的开发者耗费15分钟学习的API,而不是花60分钟学习它,结果是428人多年的净储蓄。这是一个很大的时间。

If 5 million developers spend 15 mins learning an API rather than spending 60 mins learning it, the result is a net savings of 428 man years. That's a lot of time.

大多数项目,但是,不涉及数以百万计的开发人员,或者100K + LOC。在一个典型的项目,说4开发者和50K左右禄,该组假设有很大的不同。对团队的开发人员将可以更好地了解的code如何工作的。这意味着,它使更大量感以优化用于快速生产高品质的code和用于减少错误的量,并进行变更所需要的努力。

Most projects, however, don't involve millions of developers, or 100K+ LOC. In a typical project, with say 4 developers and around 50K loc, the set of assumptions are a lot different. The developers on the team will have a much better understanding of how the code works. That means that it makes a lot more sense to optimize for producing high quality code quickly, and for reducing the amount of bugs and the effort needed to make changes.

消费1周开发code这是与.NET框架相一致,对8几个小时的时间code,很容易改变,有较少的错误可导致:

Spending 1 week developing code that is consistent with the .net framework, vs 8 hours writing code that is easy to change and has fewer bugs can result in:


  1. 后期项目

  2. 下的奖金

  3. 增加bug数量

  4. 在办公室花更多的时间和更少的时间在海滩上喝玛格丽特。

无4999999其他开发商来吸收成本,它通常是不值得的。

例如,测试为标志接口归结为一个单一的是前pression,并导致更少的code,它寻找的属性。

For example, testing for marker interfaces comes down to a single "is" expression, and results in less code that looking for attributes.

所以,我的建议是:


  1. 按照指导原则框架宗教如果您正在开发类库(或UI部件),意味着广小号$ P $垫的消耗。

  2. 考虑采用其中一些,如果你有超过10万LOC在你的项目

  3. 否则完全忽略它们。

这篇关于什么是一个标记接口的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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