This is a story about how I gradually fell in love with composition

You’re not describing composition.

Go Files do not “hasa reader”. You don’t do file.reader.read(), you just do file.read(), that’s inheritance as file has inherited the read() method.

@colonial@lemmy.world
link
fedilink
English
71Y

You’re confusing polymorphism for inheritance. read is a method on an interface that File implements - it is not inherited from a base class. You can use that File directly, or wherever a Reader interface (or whatever the name is, idk I don’t really do Go) is expected.

Composition do not necessitate the creation of a new field like x.reader or x.writer, what are you on?

https://www.infoworld.com/article/3409071/java-challenger-7-debugging-java-inheritance.html#toc-2

composition is literally the “has a” relationship. That’s how its always been taught.

@platypus_plumba@lemmy.world
link
fedilink
English
0
edit-2
1Y

Man, I honestly have no idea why they are downvoting you. Composition literally means taking common behavior and placing it in external objects behind interfaces using a has-a relationship.

No idea why they are denying this. Inheritance vs composition is the same as “is-a” vs “has-a”. In composition re usability isn’t done via inheritance but code encapsulation.

Saying that in Go objects can implement many interfaces is the Interface Segregation principle from SOLID. Basically having small interfaces instead of a big interface with methods that may not be implemented by all implementors.

Create a post

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person’s post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you’re posting long videos try to add in some form of tldr for those who don’t want to watch videos

Wormhole

Follow the wormhole through a path of communities !webdev@programming.dev



  • 1 user online
  • 1 user / day
  • 1 user / week
  • 1 user / month
  • 1 user / 6 months
  • 1 subscriber
  • 1.21K Posts
  • 17.8K Comments
  • Modlog