内部并在私有 api 中受保护

发布于 2024-10-07 18:12:22 字数 241 浏览 8 评论 0原文

我在大约 12 人的开发团队中工作,我们构建了一组合理的 API,并严格在内部使用。通常,所有类和接口都是公共的,因为这就是它们的完成方式。我经常考虑将某些构造函数设为内部的价值,以便 API 的使用者(尽管是内部的)必须使用工厂或我现在无法想到的其他一些原因。

这是您和您的团队所实践的吗?

这对您的单元测试有何影响?您是否发现可以通过类的工厂对类进行单元测试,或者您是否可以通过 PrivateObject 之类的东西访问构造函数?

I work in development team of about 12 and we build a reasonable set of API's that we use on a strictly in-house only basis. Typically all classes and interfaces are public because that is just how they get done. I have often considered the value of making some of the constructors internal so that the consumers of the API (albeit internal) have to use the factory or some other reason that I can't think of now.

Is this something that you and your team practice?

How does this effect your unit tests? Do you find that it is okay to unit test a class through it's factory or do you access the constructor through something like PrivateObject?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ㄖ落Θ余辉 2024-10-14 18:12:22

答案是肯定的;我当前的项目只有一个开发人员(我)在处理它,但我仍然根据需要使用可见性和其他访问修饰符,以及更复杂的设计模式。原因如下:

  • 如果您愿意的话,编译器可以成为您实施良好设计模式的最佳工具之一。当你必须维护一个对象时,将所有内容公开可能会在以后引起很多麻烦,而该对象在程序执行期间的正常状态就相当于一个人在手术台上度过了一生,胸口裂开了,每个人都在从他的孩子到他的电力公司都了解他,他们在他的重要器官中探查,让他做他们想做的事。添加另一只手或移除一只手可能会导致患者心脏骤停。

  • 代码应该是自记录的。被标记为内部的类或类成员意味着它可能应该是;如果一切都是公开的,你不知道在与对象交互时是否有什么东西是不应该接触的。再一次,你让你的病人坐在手术台上,突然一个新人进来,抓住肝脏并说“嘿,这是做什么的?”。对象应该握手,被告知做某事并放手去做,除了它们之外,任何人都不关心它们的肝脏功能。

  • 代码应该可以由您的子孙后代维护。这与前两条规则相关,但基本上,有人应该能够打开您的代码库,找到入口点并跟踪基本执行流程,同时查看沿途使用的对象并确定它们的一般形式和功能。回到手术台上的病人,假设五年后有人走进这个场景;一个人在桌子上裂开,他的内脏里有 50 个人。这看起来不像是他见过的任何有礼貌的社会习俗;它可能看起来最像一场活人祭祀仪式,大多数人在遇到这种情况时的第一本能就是逃跑。

然而,硬币的另一面是,为了自身的目的而实现的设计模式通常是一件非常糟糕的事情。一旦你大学毕业并找到第一份工作,没有人真正关心你是否知道如何实施战略模式,你不应该一有机会就说你知道了。每种模式都有一系列适用的情况。如果你是一名医生,你会为下一位走进来只是说你能做到的病人进行血管成形术吗?

The answer is yes; my current project has exactly one developer working on it - me - and yet I still use visibility and other access modifiers, along with more complex design patterns, as necessary. Here's why:

  • The compiler, if you let it, can be one of your greatest tools to enforce good design patterns. Making everything public can cause a lot of headaches down the road, when you have to maintain an object whose normal state during program execution is the object-oriented equivalent of a human living his life on an operating table with his chest cracked open and everyone who knows him from his children to his electric company poking around in his vital organs to get him to do what they want. Adding another hand, or removing one, can cause the patient to go into cardiac arrest.

  • Code should be self-documenting. A class or class member that is marked as internal means it probably should be; if everything's public, you don't know if there's anything you shouldn't touch when interfacing with the object. Again, you've got your patient sitting on the operating table, and all of a sudden a new guy comes in, grabs the liver and goes "hey, what does this do?". Objects should have their hands shaken, be told to do something and let go to do it, and the function of their liver is of no concern to anyone but them.

  • Code should be maintainable by your posterity. This ties back to the first two rules, but basically, someone should be able to open up your codebase, find the entrance point and trace through basic execution flow, all the while looking at objects utilized along the way and determining their general form and function. Back to our patient on the operating table, let's say five years from now someone walks in on this scene; a guy split open on a table with 50 guys in his guts. It's not going to look like any polite social custom he's ever seen; it'll probably look most like a ritual human sacrifice, and most people's first instinct when encountering such a situation is to run.

However, the flip side of the coin is that a design pattern implemented for its own sake is generally a Very Bad Thing. Once you graduate college and get your first job, nobody really cares that you know how to implement a Strategy pattern, and you shouldn't do so at the first opportunity just to say you did. Every pattern has a set of circumstances in which it applies. If you were a doctor, would you perform an angioplasty on the next patient who walked in just to say you were able to do it?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文