What is Law of Demeter?

前端 未结 3 1059
慢半拍i
慢半拍i 2021-02-20 10:05

Let\'s start with Wikipedia:

More formally, the Law of Demeter for functions requires that a method m of an object O may

3条回答
  •  走了就别回头了
    2021-02-20 10:44

    The 5th is difficult to represent in C# or Java, since they don't technically support global variables. However, in a design pattern that is similar in principle, you could have e.g. a configuration class that just contains globally-accessible static configuration values, such as (C#):

    internal class MyConfiguration
    {
        private static String MyConfigurationValue; // set in constructor
        MyConfiguration(){ MyConfigurationValue = DoSomethingToLoadValue(); }
        public static String GetMyConfigurationValue(){ return MyConfigurationValue; }
    }
    

    In this case (assuming the design pattern was acceptable in all other ways), the Law of Demeter would allow this, since it is globally accessible and intended to be that way.

提交回复
热议问题