开发者

Purpose of Constructor chaining?

开发者 https://www.devze.com 2023-04-07 22:40 出处:网络
After reading this Constructor chaining question I am just curious to know why would any one do constructor chaining?

After reading this Constructor chaining question I am just curious to know why would any one do constructor chaining?

Could some one please shed some light on type of scenarios where this might be useful.

Is this a good programming practice?开发者_Python百科


It's absolutely a good practice for two main reasons:

To avoid code duplication

class Foo
{
    public Foo(String myString, Int32 myInt){
        //Some Initialization stuff here
    }

    //Default value for myInt
    public Foo(String myString) : this(myString, 42){}

    //Default value for both
    public Foo() : this("The Answer", 42){}
}

To enforce good encapsulation

public abstract class Foo
{
    protected Foo(String someString)
    {
        //Important Stuff Here
    }
}

public class Bar : Foo
{
    public Bar(String someString, Int32 myInt): base(someString)
    {
        //Let's the base class do it's thing
        // while extending behavior
    }
}


The main reason is to be able to reuse code between constructors. Instead of duplicating initialization, you can put all of the initialization code in a single constructor, and call that one from other constructors.


‘Constructor Chaining’ is an approach whereby a constructor calls another constructor in the same class. This is particularly useful when we have a class that defines multiple constructors. For example, we are creating a class called Person in the example below. We also have three fields that we can use; Age, Name and HairColour. This class has three constructors. If we did not use the ‘Constructor Chaining’ approach, the code would be something like below:

Not Using Constructor Chaining:

public class Person
    {
        private int Age;
        private string Name;
        private string HairColour;


        public Person(int theAge)
        {
           Age = theAge;
        }

        public Person(int theAge, string theName)
        {
            Age = theAge;
            Name = theName;
        }

        public Person(int theAge, string theName, string theHairColour)
        {
            Age = theAge;
            Name = theName;
            HairColour = theHairColour;
        }

    }

As you can see, in each constructor we are assigning a value to Age, which duplicates code. We have also assigned a value to Name in two of the constructors, so again more duplication. To eliminate this problem, we can assign all the values to Age, Name and HairColour in the constructor with the most parameters. We can then call that constructor when the other two constructors are called. See the code below to see this ‘Chaining’ approach.

Using Constructor Chaining:

public class Person
    {
        private int Age;
        private string Name;
        private string HairColour;


        public Person(int theAge):this(theAge, "", "")
        {
            //One parameter
        }

        public Person(int theAge, string theName):this(theAge, theName, "")
        {
            //Two Parameters
        }

        public Person(int theAge, string theName, string theHairColour)
        {
            //Three parameters
            Age = theAge;
            Name = theName;
            HairColour = theHairColour;
        }

    }

Hope that helps - it saves on duplication.

A more 'extreme' example with lots of fields (and so lots of potential duplications) is shown here:

An example where it will definitely save on duplication


I've seen it when you've got some heavy lifting being done at construction time, and you've got lots of different ways of creating the object. (So handful of ctors with different parameter signatures).

You can just have a private member function which does the common work across ctors. You don't really need to have one ctor call another in the same class. (Which isn't even allowed in most languages).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号