Go course

InterfacesSatisfying Several Interfaces

Go

Display

Theme

Interfaces · Test exercise

Satisfying Several Interfaces

A single type can satisfy many interfaces at once — it just needs every method each interface asks for.

type Pembaca interface {
	Baca() string
}

type Penulis interface {
	Tulis(teks string)
}

type Buku struct {
	isi string
}

func (b Buku) Baca() string        { return b.isi }
func (b Buku) Tulis(teks string)   { /* ... */ }

Buku is a Pembaca (reader) and a Penulis (writer).

Combining interfaces (embedding)

You can build an interface out of other interfaces by writing them inside it (embedding):

type PembacaPenulis interface {
	Pembaca
	Penulis
}

PembacaPenulis means "has a Baca and a Tulis method". Go's standard library uses this pattern, for example io.ReadWriter, which combines io.Reader and io.Writer.

Keep interfaces small

In Go, good interfaces are usually small — often just one or two methods. Small interfaces are easy for many types to satisfy and easy to combine when you need to. As the Go proverb goes: "The bigger the interface, the weaker the abstraction."

Functions should also ask for the smallest interface they need. A function that only reads should ask for a Pembaca, not a PembacaPenulis.

Task

An RPG has two abilities, each with its own small interface (already provided):

type Penyerang interface {
	Serang() int
}

type Penyembuh interface {
	Sembuhkan() int
}

type PenyerangPenyembuh interface {
	Penyerang
	Penyembuh
}

Penyerang is an attacker (Serang = attack), Penyembuh is a healer (Sembuhkan = heal), and PenyerangPenyembuh is both. Complete the code:

  1. func (k Ksatria) Serang() int — return k.Kekuatan * 2.
  2. func (p Paladin) Serang() int — return p.Kekuatan + 5.
  3. func (p Paladin) Sembuhkan() int — return p.Iman * 3.
  4. func totalDamage(a, b Penyerang) int — the sum of Serang() from both attackers.
  5. func nilaiDukungan(x PenyerangPenyembuh) int — the sum of Serang() and Sembuhkan() from x.

Example: nilaiDukungan(Paladin{Kekuatan: 10, Iman: 4}) returns 15 + 12 = 27.

main.go
package main

import "fmt"

type Penyerang interface {
	Serang() int
}

type Penyembuh interface {
	Sembuhkan() int
}

type PenyerangPenyembuh interface {
	Penyerang
	Penyembuh
}

type Ksatria struct {
	Kekuatan int
}

type Paladin struct {
	Kekuatan int
	Iman     int
}

func (k Ksatria) Serang() int {
	// TODO
	return 0
}

func (p Paladin) Serang() int {
	// TODO
	return 0
}

func (p Paladin) Sembuhkan() int {
	// TODO
	return 0
}

func totalDamage(a, b Penyerang) int {
	// TODO
	return 0
}

func nilaiDukungan(x PenyerangPenyembuh) int {
	// TODO
	return 0
}

func main() {
	k := Ksatria{Kekuatan: 12}
	p := Paladin{Kekuatan: 10, Iman: 4}
	fmt.Println("Total damage:", totalDamage(k, p))
	fmt.Println("Nilai dukungan paladin:", nilaiDukungan(p))
}

Output

Go runtime not loaded yet

Press Run to see the output, or Submit to check your code.

Ctrl/Cmd + Enter: Run · Ctrl/Cmd + Shift + Enter: Submit · Ctrl/Cmd + S: tidy the code (gofmt)