CHAPTER 5 UNDERSTANDING ENCAPSULATION
Chaining Constructor Calls Using this
Another use of the this keyword is to design a class using a technique termed constructor chaining. This
design pattern is helpful when you have a class that defines multiple constructors. Given the fact that
constructors often validate the incoming arguments to enforce various business rules, it can be quite
common to find redundant validation logic within a class’s constructor set. Consider the following
updated Motorcycle:
class Motorcycle
{
public int driverIntensity;
public string driverName;
public Motorcycle() { }
// Redundent constructor logic!
public Motorcycle(int intensity)
{
if (intensity > 10)
{
intensity = 10;
}
driverIntensity = intensity;
}
public Motorcycle(int intensity, string name)
{
if (intensity > 10)
{
intensity = 10;
}
driverIntensity = intensity;
driverName = name;
}
...
}
Here (perhaps in an attempt to ensure the safety of the rider) each constructor is ensuring that the
intensity level is never greater than 10. While this is all well and good, you do have redundant code
statements in two constructors. This is less than ideal, as you are now required to update code in
multiple locations if your rules change (for example, if the intensity should not be greater than 5).
One way to improve the current situation is to define a method in the Motorcycle class that will
validate the incoming argument(s). If you were to do so, each constructor could make a call to this
method before making the field assignment(s). While this approach does allow you to isolate the code
you need to update when the business rules change, you are now dealing with the following redundancy:
class Motorcycle
{
public int driverIntensity;
public string driverName;
172