Skip to content

Instantly share code, notes, and snippets.

@jay-babu
Created September 30, 2022 01:58
Show Gist options
  • Save jay-babu/8432b7624729f43d64fe09c0cd4f8cf0 to your computer and use it in GitHub Desktop.
Save jay-babu/8432b7624729f43d64fe09c0cd4f8cf0 to your computer and use it in GitHub Desktop.
Single Responsibility Principle `idealImpl`
package main
import (
"encoding/json"
"fmt"
)
type CustomerInfo interface {
Name() string
Address() string
}
type Customer struct {
name string
address string
}
func (c Customer) Name() string {
return c.name
}
func (c Customer) Address() string {
return c.address
}
type CompleteOrder interface {
Complete()
}
type Order struct {
Id int
Customer CustomerInfo
complete bool
}
func (o Order) Bill() int {
fmt.Println("Billed", o.Customer.Name())
return o.Id * 2
}
func (o *Order) Complete() {
o.complete = true
}
type Delivery struct {
Order *Order
}
func (d *Delivery) Deliver() {
fmt.Printf("Order: %d delivered!\n", d.Order.Id)
d.Order.Complete()
}
func main() {
c := Customer{
name: "Bob",
address: "California",
}
o := Order{
Id: 1,
Customer: c,
}
o.Bill()
d := Delivery{
Order: &o,
}
d.Deliver()
bytes, _ := json.Marshal(d)
fmt.Println(string(bytes))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment