开发者

Do interfaces solve DDD with code duplication?

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 int开发者_StackOverflowerface 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!!!
    }
}


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.


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜