开发者

Using composition over inheritance when overriding

开发者_如何学PythonIf I have a class that I would like to customise by overriding one if its methods the only I can do this is by using inheritance (sub-classing and overriding method) ? Is it possible to use composition in some way to achieve same goal ?


Yes, you can use delegation. Instead of deriving from Foo in the example below, Bar contains a Foo and delegates to it where it chooses.

interface SomeMethods {
  void doSomething();
  void doSomethingElse();
}

class Foo implements SomeMethod {
  public void doSomething() { // implementation }
  public void doSomethingElse() { // implementation }
}

class Bar implements SomeMethod {
   private final Foo foo = new Foo();

   public void doSomething() { foo.doSomething(); }
   public void doSomethingElse() { // do something else! }
}


Using composition instead of inheritance is a design choice.

Either your class has been designed with inheritance in mind (i.e. it provides non-final public and protected methods, intended to be overridden by subclasses), or it has been designed with composition or delegation in mind (by using the strategy pattern, for example).

If the class has not been designed for customization at all, you might be able to customize it using inheritance, but not using a composition/delegation mechanism.


Sure. You can use the following patterns.

Simple overriding of method method

Template method pattern

class Base {
    public void foo() {
        // do something
        bar();
        // do something
    }
    protected abstract void bar();
}

class Child {
    protected void bar() {
        // do something.
    }
}

Delegation

class Base {
    private Runnable r;
    protected Base(Runnable r) {
        this.r = r;
    }

    public void foo() {
        r.run();
    }
}

class Child extends Base {
    Child() {
        super(new Runnable() { /* implementation */})
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜