How many constructors should a class have?

前端 未结 16 1854
一向
一向 2021-02-04 12:34

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 clas

相关标签:
16条回答
  • 2021-02-04 12:37

    Surely a class should have as many constructors as are required by the class... this doesnt mean than bad design can take over.

    Class design should be that a constructor creates a valid object after is has finished. If you can do that with 1 param or 10 params then so be it!

    0 讨论(0)
  • 2021-02-04 12:38

    The answer: 1 (with regards to injectables).

    Here's a brilliant article on the topic: Dependency Injection anti-pattern: multiple constructors

    Summarized, your class's constructor should be for injecting dependencies and your class should be open about its dependencies. A dependency is something your class needs. Not something it wants, or something it would like, but can do without. It's something it needs.

    So having optional constructor parameters, or overloaded constructors, makes no sense to me. Your sole public constructor should define your class's set of dependencies. It's the contract your class is offering, that says "If you give me an IDigitalCamera, an ISomethingWorthPhotographing and an IBananaForScale, I'll give you the best damn IPhotographWithScale you can imagine. But if you skimp on any of those things, you're on your own".

    Here's an article, by Mark Seemann, that goes into some of the finer reasons for having a canonical constructor: State Your Dependency Intent

    0 讨论(0)
  • 2021-02-04 12:38

    I generally have one, which may have some default parameters. The constructor will only do the minimum setup of the object so it's valid by the time it's been created. If I need more, I'll create static factory methods. Kind of like this:

    class Example {
    public:
      static FromName(String newname) { 
        Example* result = new Example();
        result.name_ = newname;
        return result;
      }
      static NewStarter() { return new Example(); }
    
    private:
      Example();
    }
    

    Okay that's not actually a very good example, I'll see if I can think of a better one and edit it in.

    0 讨论(0)
  • 2021-02-04 12:39

    The only "legit" case I can see from you code is if half of them are using an obsolete type that you are working to remove from the code. When I work like this I frequently have double sets of constructors, where half of them are marked @Deprecated or @Obsolete. But your code seems to be way beyond that stage....

    0 讨论(0)
  • 2021-02-04 12:39

    A constructor should have only those arguments which are mandatory for creating the instance of that class. All other instance variables should have corresponding getter and setter methods. This will make your code flexible if you plan to add new instance variables in the future.

    In fact following OO principle of -

    1. For each class design aim for low coupling and high cohesion
    2. Classes should be open for extension but closed for modification.

      you should have a design like -

      import static org.apache.commons.lang3.Validate.*; public class Employee { private String name; private Employee() {}

      public String getName() { return name; }

      public static class EmployeeBuilder { private final Employee employee;

      public EmployeeBuilder()
      {
          employee = new Employee();
      }
      
      public EmployeeBuilder setName(String name)
      {
          employee.name = name;
          return this;
      }
      
      public Employee build()
      {
          validateFields();
          return employee;
      }
      
      private void validateFields()
      {
          notNull(employee.name, "Employee Name cannot be Empty");
      }
      

      } }

    0 讨论(0)
  • 2021-02-04 12:43

    I think that a class that has more than one constructor has more than one responsibility. Would be nice to be convinced about the opposite however.

    0 讨论(0)
提交回复
热议问题