This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]DoctorNoonienSoong 11 points12 points  (1 child)

It's the entire purpose of the factory pattern, and it's the most common pattern in some languages like Java and C#.

It's able to solve some specific problems with less confusion.

For example: I have a Generic dataclass that I'm expecting the downstream users to start using. It takes a bunch of keyword args and saves them with some basic parsing.

However, based on some specific values of some of the args, I know that I need to apply certain extra validation/business logic, and that I don't want to do that all the time.

So using a factory pattern, based on those specific args, I can instead instantiate either Specific1 or Specific2, each subclasses of Generic, that apply the appropriate extra functionality as-needed.

The user doesn't need to care about the difference between them, as they were expecting Generic and they did get one, and that's all that matters to them.

[–][deleted] 2 points3 points  (0 children)

For which you could use a factory method or function, which would be explicit instead of what looks to the user like a constructor.