一个类应该有几个构造函数? [英] How many constructors should a class have?

查看:564
本文介绍了一个类应该有几个构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在修改具有9个不同构造函数的类。现在总的来说,我相信这个类的设计很糟糕……所以我想知道对于一个拥有如此多构造函数的类来说,这是否是一个糟糕的设计。

I'm currently modifying a class that has 9 different constructors. Now overall I believe this class is very poorly designed... so I'm wondering if it is poor design for a class to have so many constructors.

之所以会出现这个问题,是因为我最近在该类中添加了两个构造函数,以尝试重构和重新设计一个类(下面的代码中为SomeManager),以便它可以进行单元测试,并且不依赖于其每个方法都是静态的。但是,由于其他构造函数很方便地隐藏在班级开始下方约一百行之外,因此我在添加构造函数时没有发现它们。

A problem has arisen because I recently added two constructors to this class in an attempt to refactor and redesign a class (SomeManager in the code below) so that it is unit testable and doesn't rely on every one of its methods being static. However, because the other constructors were conveniently hidden out of view about a hundred lines below the start of the class I didn't spot them when I added my constructors.

现在发生的是调用这些其他构造函数的代码取决于SomeManager类已经被实例化,因为它曾经是静态的。空引用异常。

What is happening now is that code that calls these other constructors depends on the SomeManager class to already be instantiated because it used to be static....the result is a null reference exception.

所以我的问题是如何解决此问题?通过尝试减少构造函数的数量?通过使所有现有的构造函数都带有ISomeManager参数?

So my question is how do I fix this issue? By trying to reduce the number of constructors? By making all the existing constructors take an ISomeManager parameter?

当然,一个类不需要9个构造函数! ...哦,最重要的是,此文件中有6000行代码!

Surely a class doesn't need 9 constructors! ...oh and to top it off there are 6000 lines of code in this file!

以下是我在上面谈论的构造函数的经过审查的表示形式:

Here's a censored representation of the constructors I'm talking about above:

public MyManager()
    : this(new SomeManager()){} //this one I added

public MyManager(ISomeManager someManager) //this one I added
{
    this.someManager = someManager;
}

public MyManager(int id)
    : this(GetSomeClass(id)) {}

public MyManager(SomeClass someClass)
    : this(someClass, DateTime.Now){}

public MyManager(SomeClass someClass, DateTime someDate)
{
    if (someClass != null)
       myHelper = new MyHelper(someOtherClass, someDate, "some param");
}

public MyManager(SomeOtherClass someOtherClass)
    : this(someOtherClass, DateTime.Now){}

public MyManager(SomeOtherClass someOtherClass, DateTime someDate)
{
    myHelper = new MyHelper(someOtherClass, someDate, "some param");
}

public MyManager(YetAnotherClass yetAnotherClass)
    : this(yetAnotherClass, DateTime.Now){}

public MyManager(YetAnotherClass yetAnotherClass, DateTime someDate)
{
    myHelper = new MyHelper(yetAnotherClass, someDate, "some param");
}

更新:

感谢每个人的回应...他们的表现很棒!

Thanks everyone for your responses...they have been excellent!

我只是想告诉我我已经结束的情况

Just thought I'd give an update on what I've ended up doing.

为了解决空引用异常问题,我已经修改了其他构造函数以采用ISomeManager。

In order to address the null reference exception issue I've modified the additional constructors to take an ISomeManager.

目前,在允许重构这个特殊的课堂时,我的双手被绑住了,所以当我有空余时间时,我将其标记为要重新设计的课堂待办事项清单上的一个。此刻,我很高兴能够重构SomeManager类……它与MyManager类一样庞大和可怕。

At the moment my hands are tied when it comes to being allowed to refactor this particular class so I'll be flagging it as one on my todo list of classes to redesign when I have some spare time. At the moment I'm just glad I've been able to refactor the SomeManager class...it was just as huge and horrible as this MyManager class.

当我围绕重新设计MyManager,我将寻找一种将功能提取到两个或三个不同类中的方法...或者确保遵循SRP所需的方法。

When I get around to redesigning MyManager I'll be looking for a way to extract the functionality into two or three different classes...or however many it takes to ensure SRP is followed.

最终,我还没有得出结论,任何给定的类都有最大数量的构造函数,但我相信在这种特殊情况下,我可以创建两个或三个类,每个都有两个或三个构造函数。 p>

Ultimately, I haven't come to the conclusion that there is a maximum number of constructors for any given class but I believe that in this particular instance I can create two or three classes each with two or three constructors each..

推荐答案

一个类只能做一件事,而只能做一件事。如果它有太多构造函数,则似乎表明它在做太多事情。

A class should do one thing and one thing only. If it has so many constructors it seems to be a tell tale sign that it's doing too many things.

使用多个构造函数来强制正确创建对象实例。在多种情况下,但9似乎很多。我怀疑那里有一个接口,并且可能拖出该接口的几个实现。每个人可能都有一个到几个与他们的专业相关的构造函数。

Using multiple constructors to force the correct creation of instances of the object in a variety of circumstances but 9 seems like a lot. I would suspect there is an interface in there and a couple of implementations of the interface that could be dragged out. Each of those would likely have from one to a few constructors each relevant to their specialism.

这篇关于一个类应该有几个构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆