Golang設(shè)計模式之外觀模式講解和代碼示例
Go 外觀模式講解和代碼示例
外觀是一種結(jié)構(gòu)型設(shè)計模式, 能為復(fù)雜系統(tǒng)、 程序庫或框架提供一個簡單 (但有限) 的接口。
盡管外觀模式降低了程序的整體復(fù)雜度, 但它同時也有助于將不需要的依賴移動到同一個位置。
概念示例
人們很容易低估使用信用卡訂購披薩時幕后工作的復(fù)雜程度。 在整個過程中會有不少的子系統(tǒng)發(fā)揮作用。 下面是其中的一部分:
- 檢查賬戶
- 檢查安全碼
- 借記/貸記余額
- 賬簿錄入
- 發(fā)送消息通知
在如此復(fù)雜的系統(tǒng)中, 可以說是一步錯步步錯, 很容易就會引發(fā)大的問題。 這就是為什么我們需要外觀模式, 讓客戶端可以使用一個簡單的接口來處理眾多組件。 客戶端只需要輸入卡片詳情、 安全碼、 支付金額以及操作類型即可。
外觀模式會與多種組件進一步地進行溝通, 而又不會向客戶端暴露其內(nèi)部的復(fù)雜性。
walletFacade.go: 外觀
package main import ( "fmt" ) type WalletFacade struct { account *Account wallet *Wallet securityCode *SecurityCode notification *Notification ledger *Ledger } func newWalletFacade(accountID string, code int) *WalletFacade { fmt.Println("Starting create account") var walletFacade = &WalletFacade{ account: newAccount(accountID), securityCode: newSecurityCode(code), wallet: newWallet(), notification: &Notification{}, ledger: &Ledger{}, } fmt.Println("Account created") return walletFacade } func (w *WalletFacade) addMoneyToWallet(accountID string, securityCode int, amount int) error { fmt.Println("Start add money to wallet") err := w.account.checkAccount(accountID) if err != nil { return err } err = w.securityCode.checkCode(securityCode) if err != nil { return err } w.wallet.creditBalance(amount) w.notification.sendWalletCreditNotification() w.ledger.makeEntry(accountID, "credit", amount) return nil } func (w *WalletFacade) deductMoneyFromWallet(accountID string, securityCode, amount int) error { fmt.Println("Starting debit money from wallet") err := w.account.checkAccount(accountID) if err != nil { return err } err = w.securityCode.checkCode(securityCode) if err != nil { return err } err = w.wallet.debitBalance(amount) if err != nil { return err } w.notification.sendWalletCreditNotification() w.ledger.makeEntry(accountID, "debit", amount) return nil }
account.go: 復(fù)雜子系統(tǒng)的組成部分
package main import "fmt" type Account struct { name string } func newAccount(name string) *Account { return &Account{ name: name, } } func (a *Account) checkAccount(name string) error { if a.name != name { return fmt.Errorf("Account Name is incorrect") } fmt.Println("Account Verified") return nil }
securityCode.go: 復(fù)雜子系統(tǒng)的組成部分
package main import "fmt" type SecurityCode struct { code int } func newSecurityCode(code int) *SecurityCode { return &SecurityCode{code: code} } func (s *SecurityCode) checkCode(incomingCode int) error { if s.code != incomingCode { return fmt.Errorf("Security Code is incorrect") } fmt.Println("SecurityCode Verified") return nil }
wallet.go: 復(fù)雜子系統(tǒng)的組成部分
package main import "fmt" type Wallet struct { balance int } func newWallet() *Wallet { return &Wallet{balance: 0} } func (w *Wallet) creditBalance(amount int) { w.balance += amount fmt.Println("Wallet balance added successfully") return } func (w *Wallet) debitBalance(amount int) error { if w.balance < amount { return fmt.Errorf("Balance is not sufficient") } w.balance -= amount return nil }
ledger.go: 復(fù)雜子系統(tǒng)的組成部分
package main import "fmt" type Ledger struct{} func (s *Ledger) makeEntry(accountID, txnType string, amount int) { fmt.Printf("Make ledger entry for accountId %s with txnType %s for amount %d\n", accountID, txnType, amount) return }
notification.go: 復(fù)雜子系統(tǒng)的組成部分
package main import "fmt" type Notification struct{} func (n *Notification) sendWalletCreditNotification() { fmt.Println("sending wallet credit notification") } func (n *Notification) sendWalletDebitNotification() { fmt.Println("Sending wallet debit notification") }
main.go: 客戶端代碼
package main import ( "fmt" "log" ) func main() { fmt.Println() walletFacade := newWalletFacade("abc", 1234) fmt.Println() err := walletFacade.addMoneyToWallet("abc", 1234, 10) if err != nil { log.Fatalf("error: %s \n", err.Error()) } fmt.Println() err = walletFacade.deductMoneyFromWallet("abc", 1234, 5) if err != nil { log.Fatalf("error: %s \n", err.Error()) } }
到此這篇關(guān)于Golang設(shè)計模式之外觀模式講解和代碼示例的文章就介紹到這了,更多相關(guān)Golang 外觀模式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用Go語言中的Context取消協(xié)程執(zhí)行的操作代碼
在 Go 語言中,協(xié)程(goroutine)是一種輕量級的線程,非常適合處理并發(fā)任務(wù),然而,如何優(yōu)雅地取消正在運行的協(xié)程是一個常見的問題,本文將通過一個具體的例子來展示如何使用 context 包來取消協(xié)程的執(zhí)行,需要的朋友可以參考下2024-11-11Go語言中make和new函數(shù)的用法與區(qū)別
這篇文章介紹了Go語言中make和new函數(shù)的用法與區(qū)別,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-07-07淺談用Go構(gòu)建不可變的數(shù)據(jù)結(jié)構(gòu)的方法
這篇文章主要介紹了用Go構(gòu)建不可變的數(shù)據(jù)結(jié)構(gòu)的方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09Go語言高效I/O并發(fā)處理雙緩沖和Exchanger模式實例探索
這篇文章主要介紹了Go語言高效I/O并發(fā)處理雙緩沖和Exchanger模式實例探索,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2024-01-01