in which scenario people use factory design patter
-
we know that in factory design patter we do not create object of any class directly rather create instance of a class by factory class. The client is an object that requires an instance of another object (the product) for some purpose. Rather than creating the product instance directly, the client delegates this responsibility to the factory. Once invoked, the factory creates a new instance of the product, passing it back to the client. Put simply, the client uses the factory to create an instance of the product. i read this article https://msdn.microsoft.com/en-us/library/ee817667.aspx?f=255&MSPPError=-2147217396 i am looking for a write up on factory design patter which show me when i need to create instance by factory because we can create instance directly but in factory design pattern instance create via factory. so what is the significance of factory design patter. so if anyone search google there is many article exist on factory design pattern but i do not find any article which tell me scenario when i will not create instance directly rather will create instance via factory. so please tell me few scenario with example code from where i can see when people would not prefer creating instance of any class directly rather will create instance via factory. thanks
tbhattacharjee
-
we know that in factory design patter we do not create object of any class directly rather create instance of a class by factory class. The client is an object that requires an instance of another object (the product) for some purpose. Rather than creating the product instance directly, the client delegates this responsibility to the factory. Once invoked, the factory creates a new instance of the product, passing it back to the client. Put simply, the client uses the factory to create an instance of the product. i read this article https://msdn.microsoft.com/en-us/library/ee817667.aspx?f=255&MSPPError=-2147217396 i am looking for a write up on factory design patter which show me when i need to create instance by factory because we can create instance directly but in factory design pattern instance create via factory. so what is the significance of factory design patter. so if anyone search google there is many article exist on factory design pattern but i do not find any article which tell me scenario when i will not create instance directly rather will create instance via factory. so please tell me few scenario with example code from where i can see when people would not prefer creating instance of any class directly rather will create instance via factory. thanks
tbhattacharjee
Tridip Bhattacharjee wrote:
i am looking for a write up on factory design patter which show me when i need to create instance by factory because we can create instance directly but in factory design pattern instance create via factory. so what is the significance of factory design patter.
A nice idea would be to create a factory if there are more than a single class than can be used to perform the action. Simple and usefull example can be found in the
DbProviderFactory
. Instead of programming against the SqlConnection and SqlCommand, you program against the interfaces IDbConnection and IDbCommand. Next, let the factory decide which actual class to create. That way the user can configure suddenly whether to use the SqlConnection, an OracleConnection, or something completely different. Another nice idea for factories is that they can also be combined with the Object Pool Pattern. See theTaskFactory
for a nice example of that. It's similar to the strategy-pattern, with the difference that a factory is for creating objects.Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)
-
Tridip Bhattacharjee wrote:
i am looking for a write up on factory design patter which show me when i need to create instance by factory because we can create instance directly but in factory design pattern instance create via factory. so what is the significance of factory design patter.
A nice idea would be to create a factory if there are more than a single class than can be used to perform the action. Simple and usefull example can be found in the
DbProviderFactory
. Instead of programming against the SqlConnection and SqlCommand, you program against the interfaces IDbConnection and IDbCommand. Next, let the factory decide which actual class to create. That way the user can configure suddenly whether to use the SqlConnection, an OracleConnection, or something completely different. Another nice idea for factories is that they can also be combined with the Object Pool Pattern. See theTaskFactory
for a nice example of that. It's similar to the strategy-pattern, with the difference that a factory is for creating objects.Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)
thanks for nice answer. can you please include a article link sir which show how i can use DbProviderFactory to connect sql server ,oracle, oldedb or use dsn to connect any db. thanks
tbhattacharjee
-
thanks for nice answer. can you please include a article link sir which show how i can use DbProviderFactory to connect sql server ,oracle, oldedb or use dsn to connect any db. thanks
tbhattacharjee
I assume Google is broken for you. Seriously, you have been posting on this site long enough to indicate that you are meant to be a professional programmer. Part of being a professional programmer is having the ability to do research for yourself.
This space for rent
-
thanks for nice answer. can you please include a article link sir which show how i can use DbProviderFactory to connect sql server ,oracle, oldedb or use dsn to connect any db. thanks
tbhattacharjee
-
thanks for nice answer. can you please include a article link sir which show how i can use DbProviderFactory to connect sql server ,oracle, oldedb or use dsn to connect any db. thanks
tbhattacharjee
A simple example of using DbProviderFactory can be found here[^]. Since you already worked with databases I'm going to assume that you'll recognize most parts; note that we ask the Factory to create a connection for us, instead of creating one ourself. The factory can also create IDbCommands, but I prefer to use the factory-method that is provided in the IDbConnection-interface. Details on the class can be found on MSDN[^]. The factory does NOT make your code database vendor-independant - it just makes it easier to switch between those classes. Your C# code would no longer depend on a specific implementation, but chances are that you are using an SQL dialect that is specific to the database-server. Stick to SQL92 if you want to minimize the risc of incompatibilities.
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)