Skip to content

Commit

Permalink
Update Open/Closed Principle: change structure & mod init
Browse files Browse the repository at this point in the history
  • Loading branch information
zikwall committed May 29, 2020
1 parent 851e503 commit c8f84f4
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 65 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func AnimalSounds() {
Если теперь добавить в массив объект, описывающий новое животное, функцию `AnimalSounds` менять не придётся.
Мы привели её в соответствие с принципом открытости-закрытости.

Код: [Принцип Открытости/Закрытости](./code/solid/open-closed.go)
Код: [Принцип Открытости/Закрытости](./code/solid/open-closed/open-closed.go)

### Liskov Substitution Principle (принцип подстановки Барбары Лисков)

Expand Down
5 changes: 0 additions & 5 deletions code/solid/open-closed.go

This file was deleted.

59 changes: 59 additions & 0 deletions code/solid/open-closed/open-closed.go
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())
}
}
62 changes: 3 additions & 59 deletions main.go
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()
}

0 comments on commit c8f84f4

Please sign in to comment.