mardi 12 avril 2016

A way to implement polymorphic types without adding more code

What is the way to implement polymorphic types without adding more code (except for maybe the factory or whatever that instantiate the types)?

For example, say we want to implement a car service shop that accept gas cars and electric cars, but in the future we want it to be able to accept say trucks, electric bikes and motorbikes without having to add explicit classes for them.

I thought of making a template (or generic) base vehicle, with derivatives that are also templates for electricVehicle and gasVehicle each will implement their special methods and then we would call the vehicle ctor with the appropriate values for each vehicle.

Should they be template (or generic)? is an interface a better choice? is there a better way?

Aucun commentaire:

Enregistrer un commentaire