Constructor vs. Factory in .NET Framework

前端 未结 4 1111
长发绾君心
长发绾君心 2021-01-23 01:22

Below is an article about .net framework\'s use of patterns. I\'m not sure I understand the bolded part in the excerpt below. Is it implying if you change the details of creati

4条回答
  •  一个人的身影
    2021-01-23 01:43

    Yes, you could use constructor overloads to change the constructor without having to alter every area in the source code but that will quickly lead to a ridiculous number of constructors if you aren't very careful.

    I think the general idea of the article is hiding the implementation rather than necessarily preventing massive constructor overloads in order to account for constructor changes. The idea being the object shouldn't have knowledge of how it can be constructed.

    In the example given, you don't want to clog your Int class with how it could become an int from a string or a double so you create your Factory whose sole responsibility is to create X object from Y parameters.

    I would definitely suggest the Head First Design Patterns book as a good intro to learning Design Patterns. The examples are Java based but the logic behind them applies no matter what language you are using.

提交回复
热议问题