Utility classes.. Good or Bad?

前端 未结 8 752
暗喜
暗喜 2020-12-24 15:19

I have been reading that creating dependencies by using static classes/singletons in code, is bad form, and creates problems ie. tight coupling, and unit testing.

I

8条回答
  •  余生分开走
    2020-12-24 15:56

    They're fine as long as you design them well ( That is, you don't have to change their signature from time to time).

    These utility methods do not change that often, because they do one thing only. The problem comes when you want to tight a more complex object to another. If one of them needs to change or be replaced, it will be harder to to if you have them highly coupled.

    Since these utility methods won't change that often I would say that is not much problem.

    I think it would be worst if you copy/paste the same utility method over and over again.

    This video How to design a good API and why it matters by Joshua Bloch, explains several concepts to bear in mind when designing an API ( that would be your utility library ). Although he's a recognized Java architect the content applies to all the programming languages.

提交回复
热议问题