开发者

Server names in a Switch statement

I have multiple server names that I need to use in a switch statement, but I don't want to hardcode the values of the server names incase they change, or I need to load the software on a new server. Any ideas would be gr开发者_开发技巧eatly appreciated?


Well, it certainly sounds like you need to load them from a configuration file of some kind (or have them passed in on a command line) - but if you don't know the names ahead of time, what would you actually want to switch on?

Consider what your real goal is - are you trying to take different actions for production servers versus test servers, for example? Work out how you want to group the servers, and then you'll have more of an idea about what information you'll need to express in the configuration file.


As Jon mentioned you could use a config file which could be edited to include new servers. Can you give us a bit more information as to what you are trying to do?


You probably want a switch on the types of servers, rather than the server names.

  • Determine which kind of server types you have (e.g. mail server, database server, file server, ...).
  • Then, make a configuration file that maps the actual server names to their server types
  • Instead of writing a switch, use if-tests

Something like this:

 if (myServer.isMailServer()) ...;
 if (myServer.isDatabaseServer()) ...;

Using an if instead of a switch has the advantage that if you decide to group servers together (which you probably don't want to do, but just in case), your if-tests keep working.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜