Going bananas with loose coupling and dependency injection

后端 未结 4 1556
被撕碎了的回忆
被撕碎了的回忆 2021-01-31 20:42

With the latest additions to our dependency injection framework (annotations in spring), the marginal cost of creating DI-managed components seems to have hit some critical new

4条回答
  •  深忆病人
    2021-01-31 21:06

    The overriding principals of good OO Design do not stop at loose coupling, but also high cohesion, which gets ignored in most discussions.

    High Cohesion

    In computer programming, cohesion is a measure of how strongly-related or focused the responsibilities of a single module are. As applied to object-oriented programming, if the methods that serve the given class tend to be similar in many aspects, then the class is said to have high cohesion. In a highly-cohesive system, code readability and the likelihood of reuse is increased, while complexity is kept manageable.

    Cohesion is decreased if:

    * The functionality embedded in a class, accessed through its methods,
      have little in common.
    * Methods carry out many varied activities, often using coarsely-grained or 
      unrelated sets of data.
    

    Disadvantages of low cohesion (or "weak cohesion") are:

    * Increased difficulty in understanding modules.
    * Increased difficulty in maintaining a system, because logical changes in 
      the domain affect multiple modules, and because changes in one module 
      require changes in related modules.
    * Increased difficulty in reusing a module because most applications
      won’t need the random set of operations provided by a module.
    

    One thing that gets lost when people go crazy with IoC containers is the cohesion is lost and traceability of what and how something does something becomes a nightmare to figure out later own down the road, because all the relationships are obscured by a bunch of XML configuration files ( Spring I am looking at you ) and poorly named implementation classes.

提交回复
热议问题