有一些面向对象的工程原理,它表达了一句话:“一个类只应该知道它作为参数所使用的类的合同,或者它所使用的任何内部合同.” C中的反例是: Foo::bar( Baz* baz){ baz()-blargh()-pants()-soil
C中的反例是:
Foo::bar( Baz* baz) { baz()->blargh()->pants()->soil(); // this is bad, Foo knows about blarghs and pants }
这个原则有名字吗?而且,实际原则而不是我上面的释义会很高兴看到.
law of demeter感谢 Jim Burger说:The Law of Demeter (LoD), or Principle of Least Knowledge, is a design guideline for developing software, particularly object-oriented programs. The guideline was invented at Northeastern University towards the end of 1987, and can be succinctly summarized as “Only talk to your immediate friends.” The fundamental notion is that a given object should assume as little as possible about the structure or properties of anything else (including its subcomponents).