Factory Design Pattern (needing critique)

后端 未结 3 585
予麋鹿
予麋鹿 2021-02-02 03:49

I am putting together an explanation and code example of this design pattern, attempting to help others around me grasp it (along with helping myself to master the pattern as we

3条回答
  •  时光说笑
    2021-02-02 04:42

    I think your explanation including the real world example is good. However, I don't think your example code shows the real benefits of the pattern.

    Some possible changes:

    • I wouldn't have the enum in parallel to the types. This looks like you have to update the enum every time a type is added. It might be more appropriate to pass the System.Type. Then you can even make the factory a generic with a template argument.
    • I think the pattern is more "impressive" if you use it for creating something like a hardware interface. You'd then have a "AbstractNetworkDevice" and all your callers don't know which hardware setup you have. But the factory can create a "TcpNetworkDevice" or a "SerialNetworkDevice" or whatever based on some configuration which was made at startup.

提交回复
热议问题