Same constructor signature with different semantics?

Occasionally, I’m confronted with the problem that I have to provide some constructors in order to initialize objects with different sets of data. These sets can be mathematically transformed into each other, for example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> public Element(int x, int y, int width, int height)
{
setValues(width, height, x, y);
}
public Element(int RectTop, int RectLeft, int RectRight, int RectBottom)
{
setValues(RectRight - RectLeft, Math.Abs(RectBottom - RectTop), RectLeft, -RectTop);
}
</code>
<code> public Element(int x, int y, int width, int height) { setValues(width, height, x, y); } public Element(int RectTop, int RectLeft, int RectRight, int RectBottom) { setValues(RectRight - RectLeft, Math.Abs(RectBottom - RectTop), RectLeft, -RectTop); } </code>
        public Element(int x, int y, int width, int height)
        {
            setValues(width, height, x, y);
        }

        public Element(int RectTop, int RectLeft, int RectRight, int RectBottom)
        {
            setValues(RectRight - RectLeft, Math.Abs(RectBottom - RectTop), RectLeft, -RectTop);
        }

With such an desired overloading, which actually isn’t any because of the same signatures, it is probable that the wrong constructor is invoked, e.g. you provide the Rect-Values and the first constructor is invoked. Assuming, we would have different types, we could achieve it by swapping types with good knowledge that this is a bad practice. But that’s simply not the case in this easy example with four times int type.

Can you think of some elegant alternatives to achieve the desired behavior that we have the convenience of overloading, but with the same signature?

4

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public Element(Point topLeft, Size size)
public Element(Point topLeft, Point bottomRight)
</code>
<code>public Element(Point topLeft, Size size) public Element(Point topLeft, Point bottomRight) </code>
public Element(Point topLeft, Size size)

public Element(Point topLeft, Point bottomRight)

You could also use factory methods

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public static Element FromLeftTopWidthSize(int left, int top, int width, int height)
public static Element FromLeftTopRightBottom(int left, int top, int right, int bottom)
</code>
<code>public static Element FromLeftTopWidthSize(int left, int top, int width, int height) public static Element FromLeftTopRightBottom(int left, int top, int right, int bottom) </code>
public static Element FromLeftTopWidthSize(int left, int top, int width, int height)

public static Element FromLeftTopRightBottom(int left, int top, int right, int bottom)

Or you can use a fluent interface

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Element.Top(10).Left(28).Right(112).Bottom(101);
Element.Top(10).Left(28).Width(102).Height(73);
</code>
<code>Element.Top(10).Left(28).Right(112).Bottom(101); Element.Top(10).Left(28).Width(102).Height(73); </code>
Element.Top(10).Left(28).Right(112).Bottom(101);

Element.Top(10).Left(28).Width(102).Height(73);

You can easily use interfaces to force Left after Top, Right or Width after Left, Bottom after Right, and Height after Width. (see below)

Or, you can do the most sensible thing of all and not allow the different choices. Just choose one and stick to it.


Example of a Progressive Fluent Interface:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public class ElementBuilder : ITopSyntax, ILeftSyntax, IRightSyntax, IWidthSyntax
{
private int _top;
private int _left;
private int _right;
public ElementBuilder(int top)
{
_top = top;
}
ILeftSyntax ITopSyntax.Left(int left)
{
_left = left;
return this;
}
IRightSyntax ILeftSyntax.Right(int right)
{
_right = right;
return this;
}
IWidthSyntax ILeftSyntax.Width(int width)
{
_right = width + _left;
return this;
}
Element IRightSyntax.Bottom(int bottom)
{
return new Element(_left, _top, _right, bottom);
}
Element IWidthSyntax.Height(int height)
{
return new Element(_left, _top, _right, _top + height);
}
}
internal interface ILeftSyntax
{
IRightSyntax Right(int right);
IWidthSyntax Width(int width);
}
internal interface IRightSyntax
{
Element Bottom(int bottom);
}
internal interface IWidthSyntax
{
Element Height(int height);
}
internal interface ITopSyntax
{
ILeftSyntax Left(int left);
}
</code>
<code>public class ElementBuilder : ITopSyntax, ILeftSyntax, IRightSyntax, IWidthSyntax { private int _top; private int _left; private int _right; public ElementBuilder(int top) { _top = top; } ILeftSyntax ITopSyntax.Left(int left) { _left = left; return this; } IRightSyntax ILeftSyntax.Right(int right) { _right = right; return this; } IWidthSyntax ILeftSyntax.Width(int width) { _right = width + _left; return this; } Element IRightSyntax.Bottom(int bottom) { return new Element(_left, _top, _right, bottom); } Element IWidthSyntax.Height(int height) { return new Element(_left, _top, _right, _top + height); } } internal interface ILeftSyntax { IRightSyntax Right(int right); IWidthSyntax Width(int width); } internal interface IRightSyntax { Element Bottom(int bottom); } internal interface IWidthSyntax { Element Height(int height); } internal interface ITopSyntax { ILeftSyntax Left(int left); } </code>
public class ElementBuilder : ITopSyntax, ILeftSyntax, IRightSyntax, IWidthSyntax
{
    private int _top;
    private int _left;
    private int _right;

    public ElementBuilder(int top)
    {
        _top = top;
    }

    ILeftSyntax ITopSyntax.Left(int left)
    {
        _left = left;
        return this;
    }

    IRightSyntax ILeftSyntax.Right(int right)
    {
        _right = right;
        return this;
    }

    IWidthSyntax ILeftSyntax.Width(int width)
    {
        _right = width + _left;
        return this;
    }

    Element IRightSyntax.Bottom(int bottom)
    {
        return new Element(_left, _top, _right, bottom);
    }

    Element IWidthSyntax.Height(int height)
    {
        return new Element(_left, _top, _right, _top + height);
    }
}

internal interface ILeftSyntax
{
    IRightSyntax Right(int right);
    IWidthSyntax Width(int width);
}

internal interface IRightSyntax
{
    Element Bottom(int bottom);
}

internal interface IWidthSyntax
{
    Element Height(int height);
}

internal interface ITopSyntax
{
    ILeftSyntax Left(int left);
}

10

One way to do it would be to write a constructor that accepts a Rectangle object.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public Element(Rectangle r) { ...
</code>
<code>public Element(Rectangle r) { ... </code>
public Element(Rectangle r) { ...

Which you can then call thusly:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>var element = new Element(new Rectangle(x, y, width, height));
</code>
<code>var element = new Element(new Rectangle(x, y, width, height)); </code>
var element = new Element(new Rectangle(x, y, width, height));

Of course, now you’re back where you started, since Rectangle asks for the very same parameters that your first overload does.

If you’re committed to providing a constructor overload for the slight variation in math, you could simply add a boolean flag to make the signature unique. Something like:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public Element(int RectTop, int RectLeft, int RectRight, int RectBottom, bool isRect)
</code>
<code>public Element(int RectTop, int RectLeft, int RectRight, int RectBottom, bool isRect) </code>
public Element(int RectTop, int RectLeft, int RectRight, int RectBottom, bool isRect)

It’s a bit of a hack, and it doesn’t matter what you pass in to the bool parameter, but it does guarantee that particular overload will be called.

2

As the accepted answer (pdr) explains, the entities which you are looking for already exist in the framework, so you could easily replace two coordinates with a Point class, or two dimensions with a Size class. And the Factory approach is also a nice solution, but unfortunately that makes inheritance impossible or difficult.

In case you can’t find a more appropriate class and need to stick with the primitive types, you can still wrap your primitive types into wrapper classes:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public class Car
{
//public Car(string make) {...}
//public Car(string model) {...}
public Car(Make make) {...}
public Car(Model model) {...}
}
public class Make
{
private string Name;
public Make(string name) { this.Name = name; }
public override string ToString()
{
return this.Name;
}
}
public class Model
{
private string Name;
public Model(string name) { this.Name = name; }
public override string ToString()
{
return this.Name;
}
}
</code>
<code>public class Car { //public Car(string make) {...} //public Car(string model) {...} public Car(Make make) {...} public Car(Model model) {...} } public class Make { private string Name; public Make(string name) { this.Name = name; } public override string ToString() { return this.Name; } } public class Model { private string Name; public Model(string name) { this.Name = name; } public override string ToString() { return this.Name; } } </code>
public class Car
{
    //public Car(string make) {...}
    //public Car(string model) {...}
    public Car(Make make) {...}
    public Car(Model model) {...}
}

public class Make
{
    private string Name;
    public Make(string name) { this.Name = name; }
    public override string ToString()
    {
        return this.Name;
    }
}
public class Model
{
    private string Name;
    public Model(string name) { this.Name = name; }
    public override string ToString()
    {
        return this.Name;
    }
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật