简体   繁体   English

了解接口的命名约定

[英]understand naming conventions of interface

I want to understand what uncle Bob writes in the Clean Code book about the naming of interfaces: 我想了解Bob叔叔在Clean Code书中写的关于接口命名的内容:

These are sometimes a special case for encodings. 这些有时是编码的特殊情况。 For example, say you are building an ABSTRACT FACTORY for the creation of shapes. 例如,假设您要建立一个抽象工厂来创建形状。 This factory will be an interface and will be implemented by a concrete class. 该工厂将是一个接口,并将由一个具体的类实现。 What should you name them? 你应该给他们起什么名字? IShapeFactory and ShapeFactory? IShapeFactory和ShapeFactory? I prefer to leave interfaces unadorned. 我更喜欢不修饰界面。 The preceding I, so common in today's legacy wads, is a distraction at best and too much information at worst. 前面的I在当今的一堆旧书中很常见,充其量可以使人分心,而在最坏的情况下则可以提供过多的信息。 I don't want my users knowing that I'm handing them an interface. 我不希望我的用户知道我正在向他们提供界面。 I just want them to know that it's a ShapeFactory. 我只想让他们知道这是一个ShapeFactory。 So if I must encode either the interface or the implementation, I choose the implementation. 因此,如果必须对接口或实现进行编码,则选择实现。 Calling it ShapeFactoryImp, or even the hideous CShapeFactory, is preferable to encoding the interface. 将其命名为ShapeFactoryImp甚至是丑陋的CShapeFactory都比对接口进行编码更可取。

What is his idea of not starting the interface with an "I", and why doesn't he want users to know that he's handling them an interface? 他不使用“ I”启动界面的想法是什么,为什么他不希望用户知道他正在处理它们的界面?

The validity of this is certainly up for debate, but (as usual) I agree with Uncle Bob on this. 这样做的有效性当然有待辩论,但(与往常一样)我同意鲍勃叔叔的观点。

There should be no need for the client of an interface to know he's been given an interface rather than a concrete class, and the client code should most commonly use the interface name, so it's best that the interface name cleanly conveys the business meaning. 接口的客户端应该不需要知道已经获得了接口,而不需要知道具体的类,并且客户端代码通常应使用接口名称,因此最好使接口名称清晰地传达业务含义。 If it's necessary to encode one or the other it's best not to encode the one you want and expect people to use. 如果有必要对其中一种进行编码,则最好不要对您希望使用并希望人们使用的一种进行编码。

The "I" prefix gives no useful information, and ties you to keeping it as an interface rather than an abstract parent class in languages that distinguish the two. “ I”前缀没有提供有用的信息,并且使您将其保留为接口,而不是使用区分两者的语言的抽象父类。

He says in the quote. 他在报价中说。 It is a distraction, cluttering up the code with unnecessary "I" everywhere when you should be having clear easy to read code, and it also is giving the users too much information that they don't care about. 当您应该清楚易读的代码时,到处都是多余的“ I”,这使人分心,并给用户提供了太多他们不关心的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM