所以我对 D 感兴趣已经有一段时间了,前一段时间我把它搞砸了。我已经开始重新审视它,我真的很喜欢它试图实现的目标,但我对我最喜欢的一个 C++ 设计选项......非虚拟接口感到不安。
我喜欢这种设计的地方在于,它允许在继承层次结构的“顶部”进行前置和后置条件检查、日志记录和资源管理。这允许设计者指定一组相关类的所有通用功能,并将类的可定制部分分解为非常小的功能。它还减少了需要在子类中编写的功能量。另外,由于虚拟扩展点是私有的,它不会污染接口,或者允许用户直接调用特定于实现的函数(这真的很关键)。
有没有办法在 D 中实现这一点?
C++ 中的示例(未经测试,未编译......仅用于说明)。
class Radio{
public:
Radio( std::string id, Station defaultStation, RxChip chip)
:defaultStation(defaultStation),
id(id),
chip(chip){
}
void turnOn() {
log.trace("Radio turned on: id:[%s]", id.c_str());
doEnableRx();
doPostEnable();
setToStation(defaultStation);
}
void turnOff(){
log.trace("Radio turned off: id:[%s]", id.c_str());
doDisableRx();
doPowerOff();
}
void tune(){
log.trace("Tuning");
findAllStations();
}
void setToStation(Station target){
logStationChange(target);
doSetRxChipPassFilter(target);
}
void setChip(RxChip chip) {
rxChip = chip;
}
RxChip getChip() {
return rxChip;
}
private:
// doesn't start with "do" as this is considered a "normal" virtual function.
virtual void findAllStations(){
chip.setFrequency(chip.getLowFreq());
setChipToNextTunedPoint();
Station stat( chip.getFrequency(), tunedStations.size() );
tunedStations.push_back(stat);
}
virtual bool setChipToNextTunedPoint() {
if(chip.isTuned()) {
while( isTuned && chip.getFrequency() < chip.getHighFreq() )
chip.incrementFreq();
}
while( !chip.isTuned() && chip.getFrequency() < chip.getHighFreq() )
chip.incrementFreq();
return chip.isTuned();
}
// "do" functions are considered mandatory extension points for sub-classes
virtual void doEnableRx() = 0;
virtual void doPostEnable() = 0;
virtual void doDisableRx() = 0;
virtual void doPowerOff() = 0;
virtual void doSetRxChipPassFilter(Station target) = 0
{
//default implementation but it must be specified for use by sub-class.
chip.setFrequency(target.getLowFreq());
while( !chip.isTuned() && chip.getFrequency() < station.getHighFreq() ) {
chip.incrementFreq();
}
}
Station defaultStation;
std::vector<Station> tunedStations;
RxChip chip;
}