我正在研究桥接模式,我有一个问题。关于下面我的代码示例LightSwitch
,FanSwitch
应该是实现者还是Slider/RadialImplementation
可以作为实现者?我的问题是要知道谁必须是实施者:这两种组合似乎都可以按任何顺序工作。我不知道我是否清楚。
class Main
{
static function main()
{
new Main();
}
public function new()
{
var s:Switch;
s = new LightSwitch();
s.implementation = new SliderImplementation();
s.on();
s.off();
s.implementation = new DialImplementation();
s.on();
s.off();
s = new FanSwitch();
s.implementation = new SliderImplementation();
s.on();
s.off();
s.implementation = new DialImplementation();
s.on();
s.off();
}
}
class Switch
{
public var implementation:SwitchImplementation;
public function new(){}
public function on(){}
public function off(){}
}
class LightSwitch extends Switch
{
public override function on()
{
trace("Light");
implementation.on();
}
public override function off()
{
implementation.off();
}
}
class FanSwitch extends Switch
{
public override function on()
{
trace("Fan");
implementation.on();
}
public override function off()
{
implementation.off();
}
}
class SwitchImplementation
{
public function new(){}
public function on(){}
public function off(){}
}
class SliderImplementation extends SwitchImplementation
{
public override function on()
{
trace("> Slider Switch [on]");
}
public override function off()
{
trace("> Slider Switch [off]");
}
}
class DialImp lementation extends SwitchImplementation
{
public override function on()
{
trace("> Dial Switch [on]");
}
public override function off()
{
trace("> Dial Switch [off]");
}
}