c# - Static factory method vs public constructor -


background:

here's code i'm working on. first, base class, account class holds information account , has methods part change values of class's properties.

public class account {     private string _username; [...]      public string username { { return _username; } } [...]      public account() { }      public account(string[] args) { [...] }      public virtual void changepassword(string newpassword) { [...] } } 

then, have class when account has been created, have named activeaccount. contains of logic actions want use account possible once account has been created. classes need not included explain question; use imagination assume classes may do:

public class activeaccount : account {     private list<conversation> _conversations; [...]      public list<conversation> conversations { { return _conversations; } } [...]      private activeaccount() { }      public static activeaccount createaccount(account account)     {         // navigate url, input fields, create account, etc.     }      public override void changepassword(string newpassword)     {         // navigate url, input fields, change password, etc.          // update property using base method, if no errors.         base.changepassword(newpassword);     } } 

i've used static factory method 2 reasons. 1) want customiseable , extensible construction of object (for example, in future might have accounttemplate provide generic information create accounts; can create static factory method overload accounttemplate parameter), , 2) having parameterless constructor allows me serialize object more xml/json.

question:

however, it's come attention have public constructor accepts account parameter, performs logic , can extended overloads easily. can keep private parameterless constructor prevent parameterless construction , allow serialization.

i'm quite new programming. i'm wondering if there specific reason use static factory methods instead of public constructors, explained above. , what's preferred way of doing want do?

i wouldn't call used static factory. in eyes it's "named constructor" since resides in class , creates object of particular class.

it used make operation easier understand, e.g. compare

int value = int32.parse(somestring); int value = new int32(somestring); // doesn't exist 

the first version makes clear parses input string, second far less verbose.

update: 1 important difference between constructors , static methods int32.parse static methods can chose whether return null in case error occured or throw exception. constructor can throw exception or - , wouldn't recommend - leave object in sort of limbo state half-initialized.


a static factory used decouple classes, , make easier change implementation, example instead of instantiating database connection using new operator in code every time need database connection use factory method returns interface:

sqlconnection myconnection = new sqlconnection(connectionstring); idbconnection myconnection = myfactory.createconnection(); 

the advantage changing createconnection method can make global changes entire project, swapping databases servers or database providers without having change code in places use database connection.


Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -