Going bananas with loose coupling and dependency injection

后端 未结 4 1557
被撕碎了的回忆
被撕碎了的回忆 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 20:46
    • Why are we grouping things in classes and what should the principles be ?

    Are you stressing the, "Grouping," or the, "Classes?"

    If you're asking why are we grouping things, then I'd second Medelt's, "Maintainability," though I'd rephrase it as, "To reduce the potential cost of ripple effects."

    Consider, for a moment, not the actual coupling between your components (classes, files, whatever they may be) but the potential coupling, that is, the maximum possible number of source code dependencies between those components.

    There is a theorem which shows that, given a chain of components a - b - c - d - e, such that a depends on b, etc., the probability that changing e will then change the c component cannot be greater than the possibility that changing e will then change d. And in real software systems, the probability that changing e will affect c is usually less than the probability that changing e will affect d.

    Of course, you might say, that's obvious. But we can make it even more obvious. In this example, d has a direct dependency on e, and c has an indirect (via d) dependency on e. Thus we can say that, statistically, a system formed predominantly of direct dependencies will suffer from a greater ripple effect than a system formed predominantly from indirect dependencies.

    Given that, in the real world, each ripple effect costs money, we can say that the cost of ripple effect of an update to system formed predominantly of direct dependencies will be higher than the cost of ripple effect of an update to system formed predominantly of indirect dependencies.

    Now, back to potential coupling. It's possible to show that, within an absolute encapsulation context (such as Java or C#, where recursive encapsulation is not widely employed) all components are potentially connected to one another via either a direct dependency or an indirect dependency with a single, intermediate component. Statistically, a system which minimises the direct potential coupling between its components minimises the potential cost of ripple effect due to any update.

    And how do we achieve this distinction between direct and indirect potential coupling (as if we haven't already let the cat out of the bag)? With encapsulation.

    Encapsulation is the property that the information contained in a modelled entity is accessible only through interactions at the interfaces supported by that modelled entity. The information (which can be data or behavior) which is not accessible through these interfaces is called, "Information hidden." By information-hiding behavior within a component, we guarantee that it can only be accessed indirectly (via the interfaces) by external components.

    This necessarily requires some sort of grouping container in which some sort of finer-grained functionality can be information-hidden.

    This is why we are, "Grouping," things.

    As to why we're using classes to group things:

    A) Classes provide a language-supported mechanism for encapsulation.

    B) We're not just using classes: we're also using namespaces/packages for encapsulation.

    Regards,

    Ed.

    0 讨论(0)
  • 2021-01-31 20:53

    I can think of two reasons.

    Maintainability: You'll naturally expect some logic to go together. Logic that defines operations on one particular outside service for example a database should probably be grouped together in a logical way. You can do this in a namespace or a class.

    State and identity: objects do not only contain logic but also maintain state. Logic that is part of the interface of working with the state of a particular object should be defined on that object. Objects also maintain identity, an object that models one entity in the problem domain should be one object in your software.

    As a side-node: The state and identity argument is mostly applicable to domain objects. In most solutions I've used the IoC container mainly for the services around those. My domain objects are usually created and destroyed as part of the program flow and I usually use separate factory objects for this. The factories can then be injected and handled by the IoC container. I've had some success creating factories as wrappers around the IoC container. This way the container handles lifetime of the domain objects too.

    This is a very interesting question. If I look back at the way I've implemented things in the past I can see a trend towards smaller and more granular interfaces and classes. Things certainly got better this way. I don't think the optimal solution has one function per class though. This will effectively mean you're using an OO language as a functional language and while functional languages are very powerful there is a lot to be said for combining the two paradigms.

    0 讨论(0)
  • 2021-01-31 20:55

    Pure DI perfect universe, I think single classes+method design is ideal. In reality we need to balance the cost of that which makes it less feasible.

    Cost factors

    • Overhead of DI. Spinning up all the underlying and related underlyings for a single method is expensive. Grouping into a class allows us to offset some of that.
    • Skills - DI is new to many (myself ESPECIALLY) so understanding how to do it better or get out of old/habitual designs is tough
    • Brown field apps which have them already, it's easier/cheaper/quicker to live with them and worry about this in future green field apps

    Hopefully my newbie-ness (yes, I am filled with made up words) with DI hasn't made me completely wrong with this.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题