Direct component object

298 Views Asked by At

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?

1

There are 1 best solutions below

0
Kerri Brown On

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.