Views: 24.1K
Replies: 6
Archived
|
The ideal patternIn a software that I'm developing, I've a problem, I'm searching a solution for that, and I think that a solution can come from a design pattern, so I'm here for some suggestions.
The software must connect to a server, the server can be of a different type (for example FTP and SQL), after the connection the software must perform some actions, for example download a list of something. We have different type of server, so if the software is connected on a FTP server, the action is download a list of files, if the software is connected on a SQL server the action is a query on a table. When the software starts, it loads a configuration file, on this file there are some server informations, what happen in reality is a population of a list of ServerInfo objects, each object contains the server type, the address and the login data. I'm searching an elegant and transparent solution, I don't want code like this: Server server; if (serverInfo.type == FTP) server = new FTPServer(); else if (serverInfo.type == SQL) server = new SQLServer(); server.connect(); Thanks. Andrea Benedetti, Apr 26, 2010
|
|
Reply 1The right design pattern to apply depends really on the following:
-saleh Saleh Najar, May 11, 2010
|
|
Reply 2Sorry i just read the complete post, so I have to update.
I don't see much room for a generic base behavior in your case. Let's take an example of getting Data. server.GetData(string uriOrFilePathOrQueryStringEtcEtc) but what will it return. Let's say you choose XML, so it will be able to cover hierarchical nature of your ftp directory structure, as well as the result of a query from Sql Server. Ok case handled. What about renaming a file. Renaming a file is totally different from updating a record. parameters are totally different from each other. Ftp will include other operations like server.MakeDirectory, server.UploadFile To make all these servers adhere to a standard interface, we may end up writing a very very tricky code, I think what you were thinking of was something like this interface IServer { GetData() UpdateData() ........................................ } and then make up you concrete servers on this basis. and then implementing your concrete servers like SqlServer { GetData() UpdateData() ------------- } The problem I see is too much different parameters, and disagreement on a base interface, which rules out the application of factory method. Now as far as Strategy is concerned, I see the same problem. Syed Saqib Ali Tipu, Apr 29, 2010
|
|
Reply 3Sorry but I'm very new in this world (Design Patterns), and now I'm very confused :-(!
I don't understand if the solution is Strategy Pattern or Abstract Factory Pattern. Andrea Benedetti, Apr 27, 2010
|
|
Reply 4Yes, I do agree, with the requirements that when any one connects to server we must be able to perform operations like
Srihari Chinna, Apr 27, 2010
|
|
Reply 5I don't understand if that's the correct/right pattern for my problem, but sure, I'm here for learn.
After the application starts, and after the server selection has taken place, the app is always connected to the server. The user can perform different operations, for example, add data (a file or directory if we are connected to an FTP server) or add a value to a table (if we are connected to a SQL server). Plus, I need a global access to the server class, like a Singleton, because it will be accessed by serveral classes throughout my application. You presented a nice solution and the Strategy Pattern looks very interesting. So, am I right to think that I will need a Singleton 'wrapper' class? Thanks. Andrea Benedetti, Apr 27, 2010
|
|
Reply 6Abstract Factory Pattern is concerned about creating families of related/dependent object. As the requirement is, based on the configuration application will connect to different server. After that you will be retrieving data from the server.
So you are not really looking for the object that are getting created or you are not concerned about the object returned. Only the need to get the collection that is having strings.
There are two algorithms, connect to FTP/SQL Server and get the data. These are two different behaviors and Data. So what we need to do is implement these algorithms in to two different classes and provide an encapsulation in one classes and make them interchangeable based on the configuration. So we will need to implement Strategy pattern here. Even if in feature we can also hook-up with the new server.
I don’t see any scope for the Abstract Factory Pattern here, until unless we are developing complete custom framework for data access layer. If you had developed one, it will on the Abstract Factory.
Any suggestions, always welcome Thanks, Chinna Srihari Srihari Chinna, Apr 27, 2010
|