I'm looking for a best practice solution for creating an object whose responsibility it is to handle file storage for a single file. I want to be able to read data from the file, write to the file, delete the file etc.

I have seen the repository pattern in use but that appears to be a solution for handling multiple files. I imagine creating an object that is named after the file it will be representing, in this case RenderOutput (or similar), that has functions such as .delete() which don't reveal anything about HOW they delete or WHERE the file is stored. This means that it doesn't matter from the outside how the file is persisted.

My question is whether or not there is a pattern or such that I can follow for naming, responsibilities, functionality etc.

  • It would seem that you're talking about a class that does the same things that most languages already have for what concerns file manipulation (call read, call write, call delete without knowing how it is deleted or where it is stored, etc.). If nothing is to be added that isn't already there, you shouldn't bother making a class for it. The question you should ask yourself is what else you want it do do that isn't done already or isn't easily organized for you now. – Neil Sep 2 '15 at 12:27
  • Unless I misunderstood you you're talking about accessing the file system directly by calling functions such as File.readBytes()? I don't want classes to access the file system directly, I'd rather they called Object.Read() and are passed back the data, so that it doesn't matter if the Object class is using a file system, database or web service. I also want to add some wrapper code to the class for handling read/write errors. – chrisheseltine Sep 2 '15 at 13:45
  • So you're looking to make a wrapper for reading/writing to database/file system/web service? This would be a serious breach in single responsibility unless the database/web service are purely used in a way that would emulate a file system (given path, return data type thing). Is this what you want? – Neil Sep 2 '15 at 14:28
  • I'm looking to write a wrapper for read/writing to a single hard copy, I don't care what or where the storage is. I imagine the location of the file is embedded into the class. Later if the file is now stored in a database I can change the location and how it is accessed but from the outside it simply returns data or writes it. Does that make sense? – chrisheseltine Sep 2 '15 at 15:19
  • I think I see what you mean. I will write an answer soon. – Neil Sep 3 '15 at 6:14

So if I understand correctly, you want a sort of Data class that allows you to read and write to it without knowing the underlying details of how it works.

You can do this by using an AbstractFactory pattern which provides these Data classes for writing/reading. Each factory will have its own implementation with a method to initialize and a method to shutdown. While your file system factory won't likely need to do much, your database factory will need to establish a connection and then close it, for instance. You must be able to do all this without passing parameters to the factory. I'll be honest, I'm not too familiar with actionscript, but in Java you could say, load a configuration file and the configuration file provides connection info or whatever else required for the factory to initialize.

Each factory type would return a specific implementation of Data class that does the actual work of writing/reading from that source. My advice is that the location is provided upon calling the factory and that location remains immutable. In other words, to write to a different location, you'd need to call the factory again with a different address.

In this way you're hiding implementation details from its usage in your program, which is fine. However, this only works so long as the usage remains the same between all Data class implementations. For instance, if you're having to determine the type of a Data class implementation so you can cast it to a DatabaseData instance and then call "BulkInsert", you should not be using this pattern nor should you be hiding implementation details. The whole point is that you should only ever need an instance of Data class.

I hope that answers your question!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.