'Interface' Considered Harmful : Uncle Bob
-
Hi, In his recent blog[^] Uncle Bob come up with a situation that explains
interface
is harmful. Here is the code snippet taken from his blog.public class Subject {
private List<Observer> observers = new ArrayList<>();
private void register(Observer o) {
observers.add(o);
}
private void notify() {
for (Observer o : observers)
o.update();
}
}public class MyWidget {...}
public class MyObservableWidget extends MyWidget, Subject {
...
}Here he explains that to implement Observer pattern done correctly, compilers must allow multiple inheritance. So my question is why don't the
MyObservableWidget
implement using dependency injection like below to avoid multiple inheritance with greater degree of separation of concerns?public class MyObservableWidget extends MyWidget {
private Subject subject;
public MyObservableWidget(Subject subject) {
this.subject = subject;
}}
Of course the
Subject
class should be an abstract class. Please let me know your thoughts. Thanks, Pop -
Hi, In his recent blog[^] Uncle Bob come up with a situation that explains
interface
is harmful. Here is the code snippet taken from his blog.public class Subject {
private List<Observer> observers = new ArrayList<>();
private void register(Observer o) {
observers.add(o);
}
private void notify() {
for (Observer o : observers)
o.update();
}
}public class MyWidget {...}
public class MyObservableWidget extends MyWidget, Subject {
...
}Here he explains that to implement Observer pattern done correctly, compilers must allow multiple inheritance. So my question is why don't the
MyObservableWidget
implement using dependency injection like below to avoid multiple inheritance with greater degree of separation of concerns?public class MyObservableWidget extends MyWidget {
private Subject subject;
public MyObservableWidget(Subject subject) {
this.subject = subject;
}}
Of course the
Subject
class should be an abstract class. Please let me know your thoughts. Thanks, Pop"Ivory tower theorizing considered harmful." -- DMR
-
"Ivory tower theorizing considered harmful." -- DMR
:-D
-
Hi, In his recent blog[^] Uncle Bob come up with a situation that explains
interface
is harmful. Here is the code snippet taken from his blog.public class Subject {
private List<Observer> observers = new ArrayList<>();
private void register(Observer o) {
observers.add(o);
}
private void notify() {
for (Observer o : observers)
o.update();
}
}public class MyWidget {...}
public class MyObservableWidget extends MyWidget, Subject {
...
}Here he explains that to implement Observer pattern done correctly, compilers must allow multiple inheritance. So my question is why don't the
MyObservableWidget
implement using dependency injection like below to avoid multiple inheritance with greater degree of separation of concerns?public class MyObservableWidget extends MyWidget {
private Subject subject;
public MyObservableWidget(Subject subject) {
this.subject = subject;
}}
Of course the
Subject
class should be an abstract class. Please let me know your thoughts. Thanks, PopI gather that this article needed the help of a proctologist with a torch.
-
I gather that this article needed the help of a proctologist with a torch.
-
I gather that this article needed the help of a proctologist with a torch.
Was that a brainless question? :(
-
Was that a brainless question? :(
-
Oh that explains the hatred towards my question. But I was not trying to degrade that article. All I meant about the technical aspects of that idea. Sorry for my ignorance; I thought my solution can overcome the problem specified in the article. Thanks, Pop
-
Oh that explains the hatred towards my question. But I was not trying to degrade that article. All I meant about the technical aspects of that idea. Sorry for my ignorance; I thought my solution can overcome the problem specified in the article. Thanks, Pop
-
Thank you! :)
-
Oh that explains the hatred towards my question. But I was not trying to degrade that article. All I meant about the technical aspects of that idea. Sorry for my ignorance; I thought my solution can overcome the problem specified in the article. Thanks, Pop
Was there hatred towards your article? I was actually commenting that o' Uncle Bob seemed to have pulled his argument out of his posterior.
-
Was there hatred towards your article? I was actually commenting that o' Uncle Bob seemed to have pulled his argument out of his posterior.
Sorry I was perplexed
-
Hi, In his recent blog[^] Uncle Bob come up with a situation that explains
interface
is harmful. Here is the code snippet taken from his blog.public class Subject {
private List<Observer> observers = new ArrayList<>();
private void register(Observer o) {
observers.add(o);
}
private void notify() {
for (Observer o : observers)
o.update();
}
}public class MyWidget {...}
public class MyObservableWidget extends MyWidget, Subject {
...
}Here he explains that to implement Observer pattern done correctly, compilers must allow multiple inheritance. So my question is why don't the
MyObservableWidget
implement using dependency injection like below to avoid multiple inheritance with greater degree of separation of concerns?public class MyObservableWidget extends MyWidget {
private Subject subject;
public MyObservableWidget(Subject subject) {
this.subject = subject;
}}
Of course the
Subject
class should be an abstract class. Please let me know your thoughts. Thanks, PopHi, Thank you for the blog link. You propose what is called mixin, which is really the go to solution to avoid bloated inheritance hierarchies. The good thing about mixin of Subject is that you hide functionality and avoids to duplicate the code in Subject. The bad thing is that you still need to duplicate the code that calls Subject aka relaying. DI with mixin is also good since you make the dependency known to the outside. This opens up for implementation replacement from the outside and gives better transparency. However, Uncle Bob really wants to hide stuff and use inheritance to avoid ANY code duplication and he can't do that in a single inheritance language. I love interfaces because they form a specification layer without any implementation details. I love abstract classes because they can be implementation templates for concrete classes. I do not want to mix the 2 concerns - at all. And it IS a tad annoying when you need add mixin in you abstract classes to avoid code duplication. I wouldn't mind multiple inheritance, if the language/compiler could manage diamonds in a consistent and obvious way (No, C++ doesn't). The class hierarchy is however implementation - not specification (IMHO). Uncle Bob doesn't address that view on the interface keyword.... and I am not sure the interface keyword had that view in mind when invented.
-
Hi, Thank you for the blog link. You propose what is called mixin, which is really the go to solution to avoid bloated inheritance hierarchies. The good thing about mixin of Subject is that you hide functionality and avoids to duplicate the code in Subject. The bad thing is that you still need to duplicate the code that calls Subject aka relaying. DI with mixin is also good since you make the dependency known to the outside. This opens up for implementation replacement from the outside and gives better transparency. However, Uncle Bob really wants to hide stuff and use inheritance to avoid ANY code duplication and he can't do that in a single inheritance language. I love interfaces because they form a specification layer without any implementation details. I love abstract classes because they can be implementation templates for concrete classes. I do not want to mix the 2 concerns - at all. And it IS a tad annoying when you need add mixin in you abstract classes to avoid code duplication. I wouldn't mind multiple inheritance, if the language/compiler could manage diamonds in a consistent and obvious way (No, C++ doesn't). The class hierarchy is however implementation - not specification (IMHO). Uncle Bob doesn't address that view on the interface keyword.... and I am not sure the interface keyword had that view in mind when invented.
Keld Ølykke wrote:
No, C++ doesn't
I think it does. Why do you say it doesn't?
Keld Ølykke wrote:
class hierarchy is however implementation - not specification
Agreed. Hence, I don't think interfaces are "harmful", but what Uncle Bob was really complaining about is not being able to have multiple base classes. However, I think true separation of concerns would use neither DI nor inheritance to do his observer pattern but rather a separate "observer manager" class that does both the observing and registration/deregistration.
#SupportHeForShe
If your actions inspire others to dream more, learn more, do more and become more, you are a leader.-John Q. Adams You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun Only 2 things are infinite, the universe and human stupidity, and I'm not sure about the former.-Albert Einstein
-
Keld Ølykke wrote:
No, C++ doesn't
I think it does. Why do you say it doesn't?
Keld Ølykke wrote:
class hierarchy is however implementation - not specification
Agreed. Hence, I don't think interfaces are "harmful", but what Uncle Bob was really complaining about is not being able to have multiple base classes. However, I think true separation of concerns would use neither DI nor inheritance to do his observer pattern but rather a separate "observer manager" class that does both the observing and registration/deregistration.
#SupportHeForShe
If your actions inspire others to dream more, learn more, do more and become more, you are a leader.-John Q. Adams You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun Only 2 things are infinite, the universe and human stupidity, and I'm not sure about the former.-Albert Einstein
Well, by not consistent I mean that you can end up with compiler accepted code, but it is unknown what it will do. The "dreaded diamond" refers to the situation where class C inherits from 2 classes B1 & B2 that both inherits the same base A. C will have 2 copies of all fields from A and at runtime it is undefined which copy you address.
-
Well, by not consistent I mean that you can end up with compiler accepted code, but it is unknown what it will do. The "dreaded diamond" refers to the situation where class C inherits from 2 classes B1 & B2 that both inherits the same base A. C will have 2 copies of all fields from A and at runtime it is undefined which copy you address.
Ah, sure, I see. But you can solve that by using "virtual" inheritance. Then there's only one instance of A. Of course, you can't always do that when using third-party libraries.
#SupportHeForShe
If your actions inspire others to dream more, learn more, do more and become more, you are a leader.-John Q. Adams You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun Only 2 things are infinite, the universe and human stupidity, and I'm not sure about the former.-Albert Einstein
-
Ah, sure, I see. But you can solve that by using "virtual" inheritance. Then there's only one instance of A. Of course, you can't always do that when using third-party libraries.
#SupportHeForShe
If your actions inspire others to dream more, learn more, do more and become more, you are a leader.-John Q. Adams You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun Only 2 things are infinite, the universe and human stupidity, and I'm not sure about the former.-Albert Einstein
Yes. If C++ was a game I would call it a tad unforgiving :-)
-
Hi, In his recent blog[^] Uncle Bob come up with a situation that explains
interface
is harmful. Here is the code snippet taken from his blog.public class Subject {
private List<Observer> observers = new ArrayList<>();
private void register(Observer o) {
observers.add(o);
}
private void notify() {
for (Observer o : observers)
o.update();
}
}public class MyWidget {...}
public class MyObservableWidget extends MyWidget, Subject {
...
}Here he explains that to implement Observer pattern done correctly, compilers must allow multiple inheritance. So my question is why don't the
MyObservableWidget
implement using dependency injection like below to avoid multiple inheritance with greater degree of separation of concerns?public class MyObservableWidget extends MyWidget {
private Subject subject;
public MyObservableWidget(Subject subject) {
this.subject = subject;
}}
Of course the
Subject
class should be an abstract class. Please let me know your thoughts. Thanks, Pop#강남풀싸롱신세경,#강남풀쌀롱,#강남풀싸롱
다가미러를보시면됩니다.각종이벤트,생일파티,진급식등기분좋은날생케익&초코케익을서비스로제공!모임에는역시강남최고의서비스로초대합니다.강남풀싸롱(Ha
rdFull)신세경실장은과대광고를하지않습니다.강남어디를가셔도룸안에서부끄럽게놀지않습니다.그만큼수질과마드가보장되어있으니1%의걱정도하
지마시고,즐거운마음으로방문해주세요!강력한언니들의적극러쉬&애인모드로화끈한술자리가항상준비중입니다.고객만족1위의강남풀싸롱
&선릉물고기였습니다.강남풀싸롱매직미러의물고기는아무나와서일할수있는"풀싸롱
"이아닙니다.언니들은모두면접을통하여사이즈좋은언니들만엄선하고있으며,주1회언니들좌담회를통하여마인드교육을실시하고있습니다.또한쓰리아웃제를도입하여,손님방에서
조금의실수라도3번이상손님들께클레임이들어오면,그언니는더이상가게에출근할수없는시스템입니다^^따라서,다른곳과는달리보다좋은수질!보다기
똥찬마인드를가진언니들과화끈하게달리실수있으며,오셔서초이스한번보시면제말이거짓이아니라는것을아실수있으실겁니다^^■강남풀싸롱서비스
시간-총2시간타임(양주+맥주+고급안주set무제한)-1시간10분(란제리립서비스2번)-45분(야구장에서즐거운시간)■강남풀싸롱가격-8시이전1
인28만/2인이상30만-8시이후1인35만/2인이상32만(기타추가비용은절대없습니다.)강남풀싸롱물고기◀삼성선릉폭스&강남커피빈명
소안내강남풀싸롱물고기◀강남마카오의새이름"선릉물고기"신세경팀장경운기춤을유행시킨부산여대학생분!!역시좋네♥다시봐도예쁘다..
널리퍼뜨려주세요제가연락이닿을수있게ㅋㅋㅋ#친추다받아요#강남풀싸롱#follow[No.1]프리미엄비지니스센
터Mo,