1

我正在研究桥接模式,我有一个问题。关于下面我的代码示例LightSwitchFanSwitch应该是实现者还是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]");
    }
}
4

1 回答 1

0

您示例中的实现Slider/RadialImplementation是类。它们隐藏在通过类的抽象暴露的接口后面Light/FanSwitch

于 2012-05-31T00:07:42.240 回答