Spring4D Noobie - if I have an interface IArchive, and 2 implementations, TFileArchive and TFolderArchive, which each take a file/folder name string in the constructor, how can I create a factory that will instantiate the correct class based on the file/folder name?

Spring4D Noobie - if I have an interface IArchive, and 2 implementations, TFileArchive and TFolderArchive, which each take a file/folder name string in the constructor, how can I create a factory that will instantiate the correct class based on the file/folder name?

ie, if the string is a filename, create the file archive, if it's a folder, create the folder archive?  I created an Archive factory class that takes in the container and has a CreateArchive method that takes the file/folder name, but still don't know how to resolve the correct implementation and pass the file/folder name parameter.. I don't want to set a property because it's meant to be read only. Stefan Glienke 

P.S Wish G+ had a wiki markup format for showing code!

Comments

  1. G+ markup support would be so nice. 
    What if the string is neither a file, nor a directory name? Or the file isn't an archive?

    ReplyDelete
  2. if IsFile(Param) then CreateFileArchive else if IsDirectory(Param) then CreateDirectoryArchive ?

    Is that what you're asking? If not, show some code so that concrete advice can be provided.

    ReplyDelete
  3. Alternatively, your IArchive interface might want to declare a method that returns true for a string param, such that the TFileArchive implementation of the IArchive method returns true when given a filename, and false on anything else, and the TFolderArchive returns true for a string param when given a directory name, and false for anything else, you can then decide to call the IArchive implementation that returns true. Hope this helps :)

    ReplyDelete
  4. Vincent Parrett  I think, Stefan Glienke  will answer your question in ~3 hours. :-)

    ReplyDelete
  5. Jeroen Wiert Pluimers  if it's neither, will throw exception.

    ReplyDelete
  6. CHUA Chee Wee of course I can just create the instances myself, without involving the container.. but then I might as well not bother with DI. I want to avoid directly instantiating the specific classes myself.

    ReplyDelete
  7. Martin Wienold Yeah, I figured I would other's a shot at answering it too though ;)

    ReplyDelete
  8. Vincent Parrett I am sure I have seen something like this being done before. Maybe in one of Nick Hodges books or in a demo, don't remember which.

    ReplyDelete
  9. I would do it like this: https://bitbucket.org/snippets/sglienke/rLgoA
    Some thoughts about this by Mark Seemann: http://blog.ploeh.dk/2012/03/15/ImplementinganAbstractFactory/

    Only you can decide if you want to use it this way or the way he called manually coded factory..

    ReplyDelete
  10. I'm not seeing what advantage there is in Stefan's code, Vincent.

    It appeared to be the same type of code used in the registration of types in VCL and FMX.

    Why would you want to add additional code to achieve what could be done in less?

    ReplyDelete
  11. Stefan Glienke  Thanks once again.. for a moment there I thought I was looking at my own code as it's pretty much identical, the bit I was missing was the .Resolve('folder', [path]) - didn't know about that overload.

    ReplyDelete

Post a Comment