Better HN
topnewbestaskshowjobs
Log inSign up

princeofdevils

1 karma · joined Jul 11, 2022 (4y ago) · 1 submissions

I can resist everything except temptation.
Submissions

comment 3y ago · link · parent

I have looked at the template method pattern, but to me this just sounds like what you really want is composition.

  open class Importer {
    fun import() {
      // do common logic...
      val parsed = parse() // call specialized behaviour
    }
    open fun parse() { ... }
  }
Vs.

  class Importer(private val parser: Parser) {
    fun import() {
      // do common logic...
      val parsed = parser.Parse() // call specialized behaviour
    }
  }
In my experience, using inheritance can quickly make things worse. (Go seems to agree, there is no inheritance in Go, only composition.)