开发者

How to design a class for managing file path?

In my app, I generate some xml file for instance : "/xml/product/123.xml" where 123 is the product's id and 123.xml contains informations about this product. I also have "/xml/customer/123.xml" where 123.xml contains informations about the client ... 123

How can I manage these file paths :

  1. I create the file path directly in the serialization method ?
  2. I create 2 static classes : CustomerSerializationPathManager and ProductSerializationPathManager with 1 method : getPath(int customerID) and getPath(int productID)
  3. I create one static class : SerializationPathManager with 2 methods : getCustomerPath(int customerID) and getProductPath(int productID)
  4. Something else

I'd prefer solution 3 because if I think there's only one reason to change this class : I change the root d开发者_如何学编程irectory.

So I'd like to have your thoughts about it... thx


If you need to save the files in specific folders and the location of these files can change, then you should move this information in a configuration file and later use if from there.

You then create a class similar to a factory, with getPathForProductExports, getPathForCustomerExports etc which reads the configuration file to return the desired path.

The configuration file can be a simple .properties file:

customer_path=/xml/customer/
product_path=/xml/product/

When generating the XML (be it customer, product or whatever) you prepend the appropriate path (getPathForCustomerExports, getPathForProductExports) to the file name.

If you later change the location you just edit the config file.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜