J*****n 发帖数: 4859 | 1 A power switch is required that can turn on or off any number of appliances.
An appliance is, for example, a Blender, a Toaster, or an Oven.
Other programmers have already written those appliances and you can't change
their source code. For example, you cannot require them all to implement a
particular interface.
Fortunately, each such appliance already has two methods to turn its
particular appliance on and off.
Unfortunately, all such methods were written by different people and have
different names (e.g. Blender.swizzle(), Toaster.startToasting(), Oven.
heatUp()).
-Write three third party appliances and put them in a separate package. It
should be visible from the output of each appliance whether it is turned on
or off (in its third party vocabulary).
-Make a representation of a multi-power point. You can plug in as many
appliances as you wish and turn them on and off all at once, by turning the
multi-power point on() and off().
我看了半天,觉得既然不能改已有的代码,那就只能用template + RTTI了(因为每个
class中开关的member function不是同名的,所以要知道type,才能决定调哪个函数)。
但是这样做其实很奇怪,template讲究的是compling time polymorphsim,现在加上个
RTTI(typeid),总觉得有点不伦不类的。
请教高手如何解决。
谢谢。 |