+ 2

Factory pattern with static class method

Is this factory pattern ok ? feel free to suggest on any improvement is it good option to have factory method as static ? I believe people suggest to have this as static method over non static class method. Any reason for same ? https://code.sololearn.com/cH7kC5lIoN30/?ref=app

6th May 2021, 5:46 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
2 Réponses
0
Yep that looks good! I would however change the name of the class and method of your factory. Simply because it's important to well understand their role in bigger projects. My suggestion: "HumanFactory" as the class and "CreateHuman" for the method. It isn't getting an existing human so "CreateHuman" would be more appropriate. I personnally prefer to make static method for factories. That way I can keep them into a single call to keep the code clean.
6th May 2021, 6:11 PM
Apollo-Roboto
Apollo-Roboto - avatar
0
Thanks Apollo-Roboto ... It sounds good... Updated code with same. Yes , static makes a clean code like no requirement to create the object. Apart from this , does static method has any other importance in terms of performance?
7th May 2021, 12:16 AM
Ketan Lalcheta
Ketan Lalcheta - avatar