Created
May 2, 2023 06:17
-
-
Save reetasingh/06ea8c6d56ddc8a9720a333e24f6f838 to your computer and use it in GitHub Desktop.
strong coupling without interfaces example
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
package main | |
import "fmt" | |
type Person struct { | |
ID int | |
Name string | |
} | |
type PersonDB struct { | |
// Database connection and operations | |
} | |
func (db *PersonDB) Save(person *Person) error { | |
// Implementation for saving a person to the database | |
fmt.Printf("Saving person: %+v\n", person) | |
return nil | |
} | |
type PersonService struct { | |
db *PersonDB | |
} | |
func (s *PersonService) CreatePerson(person *Person) error { | |
// Business logic for creating a person | |
err := s.db.Save(person) | |
if err != nil { | |
return err | |
} | |
return nil | |
} | |
func main() { | |
db := &PersonDB{} | |
service := &PersonService{db: db} | |
person := &Person{ | |
ID: 101, | |
Name: "John B", | |
} | |
err := service.CreatePerson(person) | |
if err != nil { | |
fmt.Println("Error:", err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment