views:

171

answers:

2

AccountController can't extend BaseAccount and BaseController at the same time. If I make all BaseAccount or BaseController methods empty, I can have an interface, but if I implement that interface in two different places, that is, I make a contract to implement a method in two different places, I will have duplicated code. Do interfaces solve DDD with code duplication?

interface A {
    function doStuff() {
    }
}

class B implements A {
    function doStuff() {
        // a code
    }
}

class C implements A {
    function doStuff() {
        // the same code!!!
    }
}
+1  A: 

Little bit confused with your last sentance, but if you want multiple inheritance then you need to do this:

AccountController extends BaseAccount, and BaseAccount extends BaseController

BaseController
  |
BaseAccount
  |
AccountController

Using this method will enable you to access all member functions of BaseAccount and BaseController from AccountController using $this.

ILMV
+2  A: 

Interfaces solve the DDD problem because the DDD problem is related to ambiguity of implementation. Interfaces don't contain implementations, so if you inherit from a single class and multiple interfaces you can't get that ambiguity.

In the situation you described, you could get the DDD if you have methods with the same signature in BaseController and BaseAccount. If you inherit from only one of those you can't get that problem.

Perhaps you could reconsider why you want to make something both an Account and a Controller. It sounds to me a little like you are making one class do too much.

As an aside, I would recommend using names like "Controller" rather than "BaseController" because it makes it more natural when you do something like this:

Controller con = ControllerFactory.Create();

as opposed to

BaseController con = ControllerFactory.Create();

In this example, "con" isn't necessarily a BaseController. It could be any Controller subclass.

ctford
I don't care about anything else, but +1 for recommending `Controller` over `BaseController`. It makes the code much more literate and intuitive, for no cost at all.
Tom