-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update Open/Closed Principle: change structure & mod init
- Loading branch information
Showing
4 changed files
with
63 additions
and
65 deletions.
There are no files selected for viewing
This file contains 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
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package open_closed | ||
|
||
import "fmt" | ||
|
||
type Animal interface { | ||
MakeSound() string | ||
} | ||
|
||
type AnimalBase struct { | ||
name string | ||
} | ||
|
||
type Lion struct{} | ||
|
||
func (lion *Lion) MakeSound() string { | ||
return "roar" | ||
} | ||
|
||
type Squirrel struct{} | ||
|
||
func (squirrel *Squirrel) MakeSound() string { | ||
return "squeak" | ||
} | ||
|
||
type Snake struct{} | ||
|
||
func (snake *Snake) MakeSound() string { | ||
return "hiss" | ||
} | ||
|
||
func AnimalSoundsWrong() { | ||
animals := []AnimalBase{ | ||
AnimalBase{name: "lion"}, | ||
AnimalBase{name: "mouse"}, | ||
AnimalBase{name: "snake"}, | ||
} | ||
|
||
for _, animal := range animals { | ||
if animal.name == "lion" { | ||
fmt.Println("roar") | ||
} else if animal.name == "mouse" { | ||
fmt.Println("squeak") | ||
} else if animal.name == "snake" { | ||
fmt.Println("hiss") | ||
} | ||
} | ||
} | ||
|
||
func AnimalSoundsTrust() { | ||
animals := []Animal{ | ||
&Lion{}, | ||
&Squirrel{}, | ||
&Snake{}, | ||
} | ||
|
||
for _, animal := range animals { | ||
fmt.Println(animal.MakeSound()) | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,8 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
type Animal interface { | ||
MakeSound() string | ||
} | ||
|
||
type AnimalBase struct { | ||
name string | ||
} | ||
|
||
type Lion struct{} | ||
|
||
func (lion *Lion) MakeSound() string { | ||
return "roar" | ||
} | ||
|
||
type Squirrel struct{} | ||
|
||
func (squirrel *Squirrel) MakeSound() string { | ||
return "squeak" | ||
} | ||
|
||
type Snake struct{} | ||
|
||
func (snake *Snake) MakeSound() string { | ||
return "hiss" | ||
} | ||
|
||
func AnimalSoundsWrong() { | ||
animals := []AnimalBase{ | ||
AnimalBase{name: "lion"}, | ||
AnimalBase{name: "mouse"}, | ||
AnimalBase{name: "snake"}, | ||
} | ||
|
||
for _, animal := range animals { | ||
if animal.name == "lion" { | ||
fmt.Println("roar") | ||
} else if animal.name == "mouse" { | ||
fmt.Println("squeak") | ||
} else if animal.name == "snake" { | ||
fmt.Println("hiss") | ||
} | ||
} | ||
} | ||
|
||
func AnimalSoundsTrust() { | ||
animals := []Animal{ | ||
&Lion{}, | ||
&Squirrel{}, | ||
&Snake{}, | ||
} | ||
|
||
for _, animal := range animals { | ||
fmt.Println(animal.MakeSound()) | ||
} | ||
} | ||
import "goavengers/go-principles/code/solid/open-closed" | ||
|
||
func main() { | ||
AnimalSoundsWrong() | ||
AnimalSoundsTrust() | ||
open_closed.AnimalSoundsWrong() | ||
open_closed.AnimalSoundsTrust() | ||
} |