tags:

views:

54

answers:

1

Hi All

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 directory.

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

+1  A: 

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.

dpb
my only problem with this solution is that I'll see a string ".xml" in my saving method, so my saving method will still have responsibility for serialization and create file path 'even if it's only a part of it), but I may be wrong.
remi bourgarel
You are not wrong. My answer is incomplete because it does not ensure a proper separation of concerns between the classes. The solution would be to go with your number 3. Put all the data likely to change in the config file (this will allow you to change the locations without touching the code) and then, the SerializationPathManager will encapsulate ALL access to the parameters and ONLY EXPOSE THE TWO METHODS: getCustomerPath(customerID) and getProductPath(productID). An ID goes in and the proper path goes out and nobody knows about any file path or extension except SerializationPathManager.
dpb
Ok thanks for your time :)
remi bourgarel