I was design a server communication module of native application.
As starting point for now I have 3 classes:
HttpSession
— class which contain all necessary data for sending requests.ResourceManager
— class for handling loading, parsing and storage server data. It's not thread safe and needs to be used within a single thread, since there several child classes of different type dependent on content type. Each of them creates per user interaction.FileLoader
— special class for queuing files for load/upload also it restores all unfinished downloads. It's thread safe since there separate threads for loading and queuing.
So ResourceManager
and FileLoader
both using HttpSession
, I decided to use composition there, so both of them contains HttpSession
passed by ref via c-tor. Here we go to actual issue, I need to use FileLoader
inside ResourceManager
, since some of the resources requires files to be loaded. Currently I have a few thoughts in mind:
- Use composition for
FileLoader
and pass it insideResourceManager
, well that's will cause high module cohesion, but was about to keep them separated - Use new instance of
FileLoader
insideResourceManager
, will break the rule thatResourceManager
is single threaded class, also spawning new threadpool for each class instance such a bad idea. I think it would be better to stay with 1st way - Keep file URLs with required action then somehow return them from
ResourceManager
and finally pass toFileLoader
, but I don't know an elegant way to do this
My question if there better way to coordinate my classes in such a case, maybe some well-known design patterns?
UPDATE 1:
Okay, I thought about 3rd variant... so here some ways to implement that I have in mind:
- Pass function to c-tor of class that would allow me to load files, concrete implementation may be different. Disadvantage that it might be few methods
ResourseManager
require, so c-tor might have a lot of parameters. - Add methods to setup callbacks for loading. I like that you may specify callbacks only when you need it... but it would be difficult for other developers to know when you need it exactly huh. It's not appropriate and such a bad design.
- Define something like
FileUploadProtocol
interface class with all necessary methodsResourceManager
might need. I find it a cool approach, but the disadvantage is that you need to implement your own class or wrapper under your class.
ResourceLoader
was just a typo, there only 3 classes. Question edited :)