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!
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!
G+ markup support would be so nice.
ReplyDeleteWhat if the string is neither a file, nor a directory name? Or the file isn't an archive?
if IsFile(Param) then CreateFileArchive else if IsDirectory(Param) then CreateDirectoryArchive ?
ReplyDeleteIs that what you're asking? If not, show some code so that concrete advice can be provided.
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 :)
ReplyDeleteVincent Parrett I think, Stefan Glienke will answer your question in ~3 hours. :-)
ReplyDeleteJeroen Wiert Pluimers if it's neither, will throw exception.
ReplyDeleteCHUA 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.
ReplyDeleteMartin Wienold Yeah, I figured I would other's a shot at answering it too though ;)
ReplyDeleteVincent 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.
ReplyDeleteI would do it like this: https://bitbucket.org/snippets/sglienke/rLgoA
ReplyDeleteSome 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..
I'm not seeing what advantage there is in Stefan's code, Vincent.
ReplyDeleteIt 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?
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