Created
January 31, 2022 15:00
-
-
Save ynwd/17b8fa3534f4d667d65572d46914d81f to your computer and use it in GitHub Desktop.
SOLID: prinsip open/closed di golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"math" | |
) | |
type tanah interface { | |
luas(size float32) float32 | |
} | |
type lingkaran struct{} | |
func (l *lingkaran) luas(diameter float32) float32 { | |
jari2 := diameter / 2 | |
return math.Phi * jari2 * jari2 | |
} | |
type persegi struct{} | |
func (p *persegi) luas(panjang float32) float32 { | |
return panjang * panjang | |
} | |
type calculator struct { | |
t tanah | |
} | |
func (c calculator) luas(ukuran float32) float32 { | |
return c.t.luas(ukuran) | |
} | |
func main() { | |
// open for extension, but closed for modification. | |
bunder := calculator{t: &lingkaran{}} | |
persegi := calculator{t: &persegi{}} | |
fmt.Println(bunder.luas(3)) // 4 | |
fmt.Println(persegi.luas(3)) // 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment