In Scala, I am trying to design the following thing.
I have a hierarchy of Config
objects. I have around 10 different subclasses of Config
right now and this number will grow.
I want to create an interface Storage
with two methods: add(c: Config): Unit
and get(name: String): Config
to be able to add/retrieve Config
objects to an underlying storage (db, file, in-memory set, etc.).
Every ConfigImpl
object needs to be stored in its own way: e.g. ConfigImplA
objects will be stored in StorageImplA
, ConfigImplB
in StorageImplB
, etc.
Basically every ConfigImpl
sub-type has its own StorageImpl
which is independent from the others.
Finally, I want to create a top-level component MultiStorage extends Storage
class which contains internally a List[_ <: Storage]
(with every Storage containing a single type of objects). The get
of MultiStorage
will try to fetch the Config
from every Storage in the list in order, while the add
will register a new c: Config
in the storage 'storing' object of the same (sub)-type of c
:
List[_ <: Storage] storages = List(storageA, storageB, storageC, ...) \\ this is externally configured
def add(c: Config) = storages.find(s => typeOf[s] == typeOf[c]).get.add(c)
As I explained here (Cannot get type of generic object in a list) I am having troubles with type management in this kind of situation. I would like to understand if the overall design of this thing is not ideal and I should approach it differently or if there is a way to properly play with types to implement a clean solution for this problem.
Aucun commentaire:
Enregistrer un commentaire