1

I don't understand what phrase direct component object means in context of Law of Demeter's article. As I can see the term was taken from David Block's article. So, what is the term and where can I get real life examples and more info about it?

Fox Amadeus
  • 196
  • 1
  • 1
  • 11
  • 1
    Own fields/properties. `this.field.method()` - ok, `this.field.anotherField.method()` - bad. – Green_Wizard Jan 10 '18 at 09:58
  • Are you sure you about it? Lets look at the [case 1](https://en.wikipedia.org/wiki/Law_of_Demeter): method `m` may only invoke the methods of `O` itself. To my mind your example contradicts the rule. Moreover there is a use-only-one-dot rule – Fox Amadeus Jan 10 '18 at 10:45
  • See ["the law of demeter is not a dot counting exercise"](https://www.google.com.ua/search?newwindow=1&source=hp&ei=UO9VWqPHD828kwXBg6WQDw&q=the+law+of+demeter+is+not+a+dot+counting+exercise&oq=law+of+demeter+n&gs_l=psy-ab.3.3.0i22i30k1l4.5145.30888.0.34231.16.13.0.2.2.0.203.1752.0j12j1.13.0....0...1c.1.64.psy-ab..1.15.1773.0..0j0i131k1j0i10k1j0i13k1.0.JDjOfMLmTPc) and ["The Law of Demeter Doesn't Mean One Dot"](http://www.yegor256.com/2016/07/18/law-of-demeter.html) – Green_Wizard Jan 10 '18 at 10:56
  • By the way, in the wiki they "hide" one dot. `a.b.Method()` is `this.a.b.Method()`, if use more formal style. – Green_Wizard Jan 10 '18 at 11:07

1 Answers1

3

Direct component objects in this case are member variables of the class. For example:

class MyClass
{
    IService service;

    public MyClass(IService service)
    {
        this.service = service;
    }

    public void MyMethod(Param param)
    {
        // O itself
        this.AnotherMethod();

        // m's parameters
        param.Method1();

        // Any objects created/instantiated within m
        TempObject temp = new TempObject();
        temp.DoSomething();

        // O's direct component objects
        service.ProvideService();

        // violates Law of Demeter
        service.GetConfig().Update();
    }

    private void AnotherMethod()
    {
        ...
    }
}

This example shows the various method calls that are considered to be in accordance with the Law of Demeter. The member variable service in this case is a direct component object.

Kerri Brown
  • 1,157
  • 1
  • 8
  • 18