How do I explain loose coupling and information hi

2019-03-09 00:47发布

How do I explain loose coupling and information hiding to a new programmer? I have a programmer who I write designs for, but who can't seem to grasp the concepts of loose coupling and information hiding.

I write designs with everything nicely broken down into classes by function (data access is separate, a class for requests, a controller, about 5 classes total). They come back with a modified design where half the classes inherit from the other half (and there is no "is-a" relationship), and many public variables.

How can I get across the idea that keeping things separate makes it easier to maintain?

标签: oop
19条回答
淡お忘
3楼-- · 2019-03-09 01:29

Show him this presentation. Though it's mainly about DI, it's downright brilliant and up to the point.

查看更多
The star\"
4楼-- · 2019-03-09 01:29

loose coupling means the external code should use the object of derived non abstract class through abstract base class. if any change occur in set of class on which it depend then not neccessory to change in external code i.e. external code really exhibit loose coupling.

查看更多
相关推荐>>
5楼-- · 2019-03-09 01:30

alt text

  • Loose coupling: The parts of a watch may be replaced by others with out breaking the whole watch. For instance you can remove one hand and it will still work.

  • Information hiding: The clock hands doesn't know that behind them there's a machinery.

Additional concept

  • High cohesion: All the elements in the watch "module" are strongly related. In this specific scenario, a battery would belong to another module or namespace.
查看更多
干净又极端
6楼-- · 2019-03-09 01:30

I would try sitting down with him and working through a couple of peieces of code with him looking over your sholder and you explaining why you are doing what you are doing as you go along. I've found this is normally the best way to explain things.

查看更多
神经病院院长
7楼-- · 2019-03-09 01:33

There's nothing like a physical analogy. Walk out to your car and point out how everything complicated, hot and dangerous is pretty well isolated from the fragile human. Sit in the driver's seat and point out some of the important gauges; for example, the coolant temperature, tachometer and speedometer. Note how the gauges are remarkably similar: they all take a scalar value (from somewhere) and represent it by moving a needle to a position between min and max.

However, if you think about what's being measured, the strong motivation to maintain that isolation (aka loose coupling or information hiding) becomes a lot more obvious.

  • "How would you like to measure the coolant temperature? By looking at a gauge or by sticking your finger into near-boiling liquid?"

  • "How would you like to measure the engine rotational speed? By looking at a gauge or by letting a multi-thousand RPM crankshaft rip the flesh from your bones as you try to estimate it by hand?"

  • "How would you like to measure the car's speed? By looking at a gauge or by dragging your foot on the ground as you're roaring down the highway?"

From there, you can build on the concepts of "your coolant temperature gauge is-a gauge. It isn't-a boiling liquid" and so forth to more complicated concepts.

查看更多
登录 后发表回答