vendredi 7 juillet 2017

Design pattern with methods that return modified parent

Lets say I have a class that looks like this:

class Data
{
    public Data()
    {
       // Stuff
    }

    public void Process1()
    {
       // Stuff
    }

    public void Process2()
    {
       // Stuff
    }
}

I have several options for how to use it. The most common one is the following:

var data = new Data();
data.Process1();
data.Process2();

One alternative that I like to use requires me to modify the class:

class Data
{
    public Data()
    {
       // Stuff
    }

    public Data Process1()
    {
       // Stuff
       return this;
    }

    public Data Process2()
    {
       // Stuff
       return this;
    }
}

Then I can use it like so:

var data = new Data().Process1().Process2();

To which design pattern, if any, does this style of writing classes belong?

Aucun commentaire:

Enregistrer un commentaire