Reputation: 1467
I have a Factory that has shops that work on unrelated things. Some shops work or personal autos and classes derived from there ( cars, SUVs, mopeds ) and others work on military vehicles and classes derived from there ( tanks, planes, submarines ).
This question relates to my Factory class, which doesn't care about what is being made in the shops, but it does provide some services to make sure the right types of vehicles are being routed to the right shops. i.e., if a Audi A4 shows up at the loading dock, it needs to be identified as a personal auto and routed to the personal auto shop.
Can I do this without declaring the types of vehicles possible in the Factory class?
So, in more C++ coding specifics, I thought my Factory could have a std::map<shop *, const std::type_info *>
container where where the std::type_info *
was provided by the shops, and points to the base class of vehicle (e.g., personal auto, or military vehicle).
But..., I have not been able to figure out a way to check if one std::type_info
for an object is the same as a candidate OR derived from that candidate.
If the Factory knew of at least the base vehicle types (personal auto and military vehicle), I am aware of how to use dynamic_cast<>()
acting on personal auto pointers or military vehicle pointers to check for type equality or that one is derived from the base. However, I am hoping to make the factory as general as possible.
Any ideas?
Upvotes: 1
Views: 470
Reputation: 1467
At this point I take it that the type of operation on std::type_info objects I want is not possible.
I think the next best option is to push the identification responsibility down to the shops. The shops maintain a table of specific products made and their type identification. e.g.
std::list<const std::type_info *> productsMadeHere;
The shops also provide a common interface that the factory can use to poll the shop if it accepts a specific type.
Shop::isThisMadeHere(std::type_info * product_type );
Upvotes: 0
Reputation: 3658
Why not letting the shops tell who they are? Create an interface that every shop has to implement, with methods based on rules like
virtual bool CanBeShipped() const = 0;
virtual bool IsMilitary() const = 0;
so that the factory has only the knowledge of the rules it needs to implement.
Upvotes: 1