'Interface' Considered Harmful : Uncle Bob
-
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,