Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Design and Architecture
  4. 'Interface' Considered Harmful : Uncle Bob

'Interface' Considered Harmful : Uncle Bob

Scheduled Pinned Locked Moved Design and Architecture
questioncomregexoopdiscussion
18 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    popchecker
    wrote on last edited by
    #1

    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

    P P K 강 4 Replies Last reply
    0
    • P popchecker

      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

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      "Ivory tower theorizing considered harmful." -- DMR

      P 1 Reply Last reply
      0
      • P PIEBALDconsult

        "Ivory tower theorizing considered harmful." -- DMR

        P Offline
        P Offline
        popchecker
        wrote on last edited by
        #3

        :-D

        1 Reply Last reply
        0
        • P popchecker

          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

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          I gather that this article needed the help of a proctologist with a torch.

          L P 2 Replies Last reply
          0
          • P Pete OHanlon

            I gather that this article needed the help of a proctologist with a torch.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Pete O'Hanlon wrote:

            with a torch

            Or maybe just an ear-trumpet? X|

            1 Reply Last reply
            0
            • P Pete OHanlon

              I gather that this article needed the help of a proctologist with a torch.

              P Offline
              P Offline
              popchecker
              wrote on last edited by
              #6

              Was that a brainless question? :(

              L 1 Reply Last reply
              0
              • P popchecker

                Was that a brainless question? :(

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Not completely, but it's more a Lounge type discussion, than a technical question.

                P 1 Reply Last reply
                0
                • L Lost User

                  Not completely, but it's more a Lounge type discussion, than a technical question.

                  P Offline
                  P Offline
                  popchecker
                  wrote on last edited by
                  #8

                  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

                  L P 2 Replies Last reply
                  0
                  • P popchecker

                    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

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    I think the "hatred" was directed at Uncle Bob, not at you.

                    P 1 Reply Last reply
                    0
                    • L Lost User

                      I think the "hatred" was directed at Uncle Bob, not at you.

                      P Offline
                      P Offline
                      popchecker
                      wrote on last edited by
                      #10

                      Thank you! :)

                      1 Reply Last reply
                      0
                      • P popchecker

                        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

                        P Offline
                        P Offline
                        Pete OHanlon
                        wrote on last edited by
                        #11

                        Was there hatred towards your article? I was actually commenting that o' Uncle Bob seemed to have pulled his argument out of his posterior.

                        P 1 Reply Last reply
                        0
                        • P Pete OHanlon

                          Was there hatred towards your article? I was actually commenting that o' Uncle Bob seemed to have pulled his argument out of his posterior.

                          P Offline
                          P Offline
                          popchecker
                          wrote on last edited by
                          #12

                          Sorry I was perplexed

                          1 Reply Last reply
                          0
                          • P popchecker

                            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

                            K Offline
                            K Offline
                            Keld Olykke
                            wrote on last edited by
                            #13

                            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.

                            T 1 Reply Last reply
                            0
                            • K Keld Olykke

                              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.

                              T Offline
                              T Offline
                              TheGreatAndPowerfulOz
                              wrote on last edited by
                              #14

                              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

                              K 1 Reply Last reply
                              0
                              • T TheGreatAndPowerfulOz

                                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

                                K Offline
                                K Offline
                                Keld Olykke
                                wrote on last edited by
                                #15

                                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.

                                T 1 Reply Last reply
                                0
                                • K Keld Olykke

                                  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.

                                  T Offline
                                  T Offline
                                  TheGreatAndPowerfulOz
                                  wrote on last edited by
                                  #16

                                  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

                                  K 1 Reply Last reply
                                  0
                                  • T TheGreatAndPowerfulOz

                                    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

                                    K Offline
                                    K Offline
                                    Keld Olykke
                                    wrote on last edited by
                                    #17

                                    Yes. If C++ was a game I would call it a tad unforgiving :-)

                                    1 Reply Last reply
                                    0
                                    • P popchecker

                                      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

                                      강 Offline
                                      강 Offline
                                      강남풀싸롱신세경실장
                                      wrote on last edited by
                                      #18

                                      #강남풀싸롱신세경,#강남풀쌀롱,#강남풀싸롱

                                      다가미러를보시면됩니다.각종이벤트,생일파티,진급식등기분좋은날생케익&초코케익을서비스로제공!모임에는역시강남최고의서비스로초대합니다.강남풀싸롱(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

                                      ,

                                      1 Reply Last reply
                                      0
                                      Reply
                                      • Reply as topic
                                      Log in to reply
                                      • Oldest to Newest
                                      • Newest to Oldest
                                      • Most Votes


                                      • Login

                                      • Don't have an account? Register

                                      • Login or register to search.
                                      • First post
                                        Last post
                                      0
                                      • Categories
                                      • Recent
                                      • Tags
                                      • Popular
                                      • World
                                      • Users
                                      • Groups