装饰一个继承受保护的observable的类

时间:2013-10-22 07:49:45

标签: java design-patterns inheritance anti-patterns javafx-8

假设我想装饰一个继承受保护的可观察字段的类。如何获得对该受保护变量的访问权限,以便扩展所述超类的功能?

请参阅下面的更具体的示例。

Class SuperSuper - 最初包含匿名观察者的类

package javafxapplication1;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.stage.Stage;

public class SuperSuper extends Application {

    protected Button button = new Button();

    @Override
    public void start(Stage stage) throws Exception {
        button.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent t) {
                doSomething();
            }
        });
    }

    public String doSomething() {
        return "1";
    }
}

Class Super - 继承匿名观察者的超类

package javafxapplication2;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.stage.Stage;
import javafxapplication1.SuperSuper;

public class Super extends SuperSuper {

    @Override
    public void start(Stage stage) throws Exception {
        button.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent t) {
                doSomething();
            }
        });
    }

    @Override
    public String doSomething() {
        return "2";
    }
}

Class SuperDecorator - Super的装饰类

package javafxapplication3;

import javafxapplication2.Super;


class SuperDecorator extends Super {
    Super zuper;

    public SuperDecorator(Super zuper){
        this.zuper = zuper;
    }

    @Override
    public String doSomething() {
        return zuper.doSomething()+"3";
    }

    //What should I put here in order to make zuper's button print 3?
}

1 个答案:

答案 0 :(得分:2)

//What should I put here in order to make zuper's button print 3?

为了达到您的要求,您不需要Super仅装饰extends才能正常工作:

class SuperDecorator extends Super {
    @Override
    public void doSomething() {
        System.out.println("3");
    }
}

然后拨打new SuperDecorator().doSomething()

理想情况下,装饰器实现应该通过interface,你的修改后的方法是正确的虽然extends仍然不能正常工作,因为在装饰过程中如何使用继承,因为方法被委托给{{1} }和结果已修改(zuper)。考虑以下备选方案:

decorated

注意:了解您的装饰在public class SuperSuper extends Application implements IActionPerformer { @Override public void start(Stage stage) throws Exception { button.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent t) { doSomething(); } }); } public String doSomething() { return "1"; } } public class Super extends SuperSuper { public String doSomething() { return "2"; } } abstract class AbstractSuperDecorator implements IActionPerformer { protected IActionPerformer zuper; public AbstractSuperDecorator(IActionPerformer zuper) { this.zuper = zuper; } public String doSomething() { return zuper.doSomething(); } } public class Super3Decorator extends AbstractSuperDecorator { public Super3Decorator(IActionPerformer zuper) { super(zuper); } public String doSomething() { return super.doSomething()+"some-val"; //decoration } } 附近的差异,该差异已移至doSomething,而不是interfacestart 。对button的{​​{1}}依赖是隐含的(实现驱动),并且它不是装饰参与者。