views:

52

answers:

2

I have a list of services which can be identified by names. Every service has a set of parameters (IP address, port, availability and so on). There are also method which can be applied to the services (close connection, open connection, send a message to the server, check if it is responding and so on).

So, I thought it is a natural approach to create a Server class and represent real servers as objects of this class.

But than I realized that it is not really convenient. For example I have a name of the server (just a string) and I would like to do something with this server. Then I need to have a map which maps name of the server to the object representing this server? It does not seems to be an elegant solution.

What I decided is to have a class containing a set of static methods. And then, for example to use it in the following way: ServerClass.sendMessage("NameOfServer","MyMessage") or for example ServerClass.close("NameOfServer") or ServerClass.getIP("NameOfServer").

Is it a good solution?

+2  A: 

An advantage of having a class with various instances is that it provides a kind of type safety. If you have

Server myServer = ServerRepository.getServer("NameOfServer");
if (myServer != null) myServer.sendMessage("MyMessage");

then you know before you send the message if your server name has a typo in it (because your repository can't return a matching message).

JacobM
My problem is the following. At some moment I see that I need to do with the server named "FirstServer". I know that for this server I have already an object. But now I need to fined this object. How should I do it? Should I have some kind of mapping from server names to objects representing servers? I do not see an elegant solution for that. It is why I thought that it is good idea to use static methods. In this case, if I know the name of the server I immediately can do things like that `MyClass.doWhatIWant("NameOfServer")`.
Roman
That's the purpose of a ServerRepository. It's a singleton object that you use to get instances of servers. When you call it, if it hasn't already created an instance of that particular server, it creates it and caches it. If you ask for it again, you get the cached instance.
JacobM
OK. I see. That's what I need. So, I need 2 classes (one is Server and another one is ServerRepository). But I do not understand how ServerRepositiory checks if there is already an object of the Server class corresponding to a specific name of the server.
Roman
ServerRepository has a way to create servers, but also keeps a map of server name to server. So when you ask for a server, it looks it up in the map, and if it's not there, it creates it (and puts it in the map). I don't think there's anything inelegant about having such a map IF it's wrapped in a repository structure and if the servers are created on an as-needed basis.
JacobM
A: 

Do all of your servers expose the same services, or are there some that are dependent on the server. As an example if you have both FooServers which have a method doFoo() and BarServers with a method doBar() but Foo has no doBar and Bar has no doFoo, then this is likely a bad idea as your ServerClass will potentially expose methods that are meaningless to potential callers. If however you know all of your servers are going to be FooServers than this may be a valid approach as you can centralize common code. I would say be careful that your code remains maintainable and you are not forcing common behavior where it needs to be customized, or you end up adding a multitude of extra arguments to indicate "special cases" where you need to the behavior to vary slightly for one reason or another.

M. Jessup
My servers a very similar to each other. They share the same methods and possess the same set of parameters. But the problem is that they are identified by names. For example, at some stage I know that I need to do something with the server called "FirstServer". And then I have to find an object which corresponds to this server. It is why I think that I probably should not represent servers as objects of a class.
Roman