Skip to content

Commit

Permalink
add facade
Browse files Browse the repository at this point in the history
  • Loading branch information
cndoit18 committed Jun 16, 2023
1 parent 01565cf commit 706ebd5
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
40 changes: 40 additions & 0 deletions design-patterns/facade/facade.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package facade

import (
"fmt"
"io"
)

type OrderSystem struct {
out io.Writer
}

func (o *OrderSystem) Settlement() float64 {
fmt.Fprintf(o.out, "Settlement completed\n")
return 20.0
}

type PaymentSystem struct {
out io.Writer
}

func (p *PaymentSystem) Payment(amount float64) {
fmt.Fprintf(p.out, "Payment of $%.2f\n", amount)

}

type ShopSystem struct {
order *OrderSystem
payment *PaymentSystem
}

func (s *ShopSystem) Buy() {
s.payment.Payment(s.order.Settlement())
}

func NewShopSystem(out io.Writer) *ShopSystem {
return &ShopSystem{
order: &OrderSystem{out: out},
payment: &PaymentSystem{out: out},
}
}
15 changes: 15 additions & 0 deletions design-patterns/facade/facade_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package facade_test

import (
"os"

"github.com/cndoit18/cs_study_plan/design-patterns/facade"
)

func ExampleNewShopSystem() {
shopSystem := facade.NewShopSystem(os.Stdout)
shopSystem.Buy()
// Output:
// Settlement completed
// Payment of $20.00
}

0 comments on commit 706ebd5

Please sign in to comment.