What are the best practices for determining the tasks of Constructor, Initialization and Reset methods

前端 未结 5 1729
长发绾君心
长发绾君心 2021-01-22 08:38

This is a general OOP question although I am designing in Java. I\'m not trying to solve a particular problem, just to think through some design principles.
From my experien

5条回答
  •  我在风中等你
    2021-01-22 09:10

    Can't say that I've ever used this exact pattern, but I've used similar things to reduce code duplication. For example when you have an object that may be either created via a constructor or from a another object (like a DTO) via a factory method. In that case, I'll often have an internal initializer that populates the properties of the object that is used by both. Can't say that I've ever used a "reset" method, nor do I see a real need for one if all it does is replicate the process of creating a new object.

    Lately, I've moved to only using default constructors and using property settors to initialize the object. The new C# syntax that allows to easily do this in a "constructor-like" format makes this every easy to use and I find the need to support parameterized constructors disappearing.

提交回复
热议问题