2

可能性があるため、オブジェクトを使用してあるクラスから別のクラスを指してはならないというルールについて、ずっと前に読みましたnull

 class Foo
 {
   public Bar Bar1 {get; set;}

   public Foo()
   {

   }

 }

 class Bar
 {
     public string Name{ get; set;}
 }


 Foo foo = new Foo();
 foo.Bar1.Name;

このパターンにちなんで法または規則は何と呼ばれますか?

4

2 に答える 2

10

It's the Law of Demeter

To quote some of it:

In particular, an object should avoid invoking methods of a member object returned by another method. For many modern object oriented languages that use a dot as field identifier, the law can be stated simply as "use only one dot". That is, the code a.b().Method() breaks the law where a.Method() does not.

于 2012-02-04T21:24:54.863 に答える