静态内部类是一个好主意还是设计不好? [英] Are static inner classes a good idea or poor design?

查看:144
本文介绍了静态内部类是一个好主意还是设计不好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现我有几个地方有公共静态内部类设计扩展帮助类使我的代码更多的类型安全,在我看来,可读。例如,假设我有一个SearchCriteria类。对于我搜索的不同的东西(一个搜索词,然后一组搜索词类型,日期范围等)有很多共性。通过在静态内部类中扩展它,我将扩展和可搜索类具有特定的区别。这似乎在理论上是一个坏主意(紧耦合坏!),但扩展是专门针对这个可搜索类(一类,一个目的)。

I'm find I have several places that having public static inner classes designed that extend "helper" classes makes my code a lot more type safe and, in my opinion, readable. For example, imagine I have a "SearchCriteria" class. There are a lot of commonalities for the different things I search for (a search term and then a group of search term types, a date range, etc.) By extending it in a static inner class, I tightly couple the extension and the searchable class with the specific differences. This seems like a bad idea in theory (Tight Coupling Bad!) but the extension is specific to this searchable class (One Class, One Purpose).

我的问题是,根据你的经验,使用静态内部类(或任何你的语言equinlent)使你的代码更可读/可维护或最终。

My question is, in your experience, has the use of static inner classes (or whatever your language equivelent is) made your code more readable/maintainable or has this ended up biting you in the EOF?

此外,我不确定这是不是社区wiki材料。

Also, I'm not sure if this is community wiki material or not.

推荐答案

听起来很合理。

紧密耦合只有在你耦合的情况下才会变得糟糕。事情不真正属于一起只是因为其中一个恰好调用另一个。对于密切合作的课程,当你在你的情况下,其中一个存在支持另一个,那么它被称为凝聚力,这是一个很好的事情。

Tight coupling is only bad when you couple things that don't really belong together just because one of them happens to call the other one. For classes that collaborate closely, e.g. when, as in your case, one of them exists to support the other, then it's called "cohesion", and it's a good thing.

这篇关于静态内部类是一个好主意还是设计不好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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