Re: Design question: polymorphism after object creation
"Marcel M?ller" <news.5.maazl@spamgourmet.com>
I am seeking for a neat solution for the following problem:
There are objects of different types with a common base class, let's say
folders and files (although that does not completely hit the nail on the
head). These objects have a common primary key, the full qualified path.
But the polymorphic type of the objects emerges only *after* the object
has been referenced. See below.
[...]
I get this, but the following seem contradictory, especially the observer
part.
If the object is not there yet why on earth is it observed?
From my limited understanding of the problem, I'd have something like:
map<string, shared_ptr<Base> > repository;
As the only info up front is PK, in this phase you just put that in the map,
the ptr is null, signaling the not-accessed state.
When access comes, you have the extra info, create the object by factory and
put it in the ptr.
If your initial info is more elaborate, you can create a struct of it, and
use instead of string in the map. To avoid duplication, the object body
can have a pointer to the PK part, passed in ctor. If the object lives only
in this repository, and it is a map, the nodes are stable. (certainly I
would internally firewall the access to PK by a function, so later it can be
simply converted in a local copy, or a shared_ptr instead of raw, etc...)