Last active
April 27, 2023 19:48
-
-
Save theartofdevel/0531b7dd4f2074de9133941fbc610eba to your computer and use it in GitHub Desktop.
Pattern Strategy in Golang for youtube channel The Art of Development
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 | |
func main() { | |
product := "vehicle" | |
payWay := 3 | |
var payment Payment | |
switch payWay { | |
case 1: | |
payment = NewCardPayment("12345", "12345") | |
case 2: | |
payment = NewPayPalPayment() | |
case 3: | |
payment = NewQIWIPayment() | |
} | |
processOrder(product, payment) | |
} | |
func processOrder(product string, payment Payment) { | |
// ... implementation | |
err := payment.Pay() | |
if err != nil { | |
return | |
} | |
} | |
// ---- | |
type Payment interface { | |
Pay() error | |
} | |
// ---- | |
type cardPayment struct { | |
cardNumber, cvv string | |
} | |
func NewCardPayment(cardNumber, cvv string) Payment { | |
return &cardPayment{ | |
cardNumber: cardNumber, | |
cvv: cvv, | |
} | |
} | |
func (p *cardPayment) Pay() error { | |
// ... implementation | |
return nil | |
} | |
type payPalPayment struct { | |
} | |
func NewPayPalPayment() Payment { | |
return &payPalPayment{} | |
} | |
func (p *payPalPayment) Pay() error { | |
// ... implementation | |
return nil | |
} | |
type qiwiPayment struct { | |
} | |
func NewQIWIPayment() Payment { | |
return &qiwiPayment{} | |
} | |
func (p *qiwiPayment) Pay() error { | |
// ... implementation | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment