欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Golang設(shè)計模式之外觀模式講解和代碼示例

 更新時間:2023年06月26日 08:27:25   作者:demo007x  
外觀是一種結(jié)構(gòu)型設(shè)計模式, 能為復(fù)雜系統(tǒng)、 程序庫或框架提供一個簡單 (但有限) 的接口,這篇文章就給大家詳細介紹一下Golang的外觀模式,文中有詳細的代碼示例,具有一定的參考價值,需要的朋友可以參考下

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語言中的Context取消協(xié)程執(zhí)行的操作代碼

    在 Go 語言中,協(xié)程(goroutine)是一種輕量級的線程,非常適合處理并發(fā)任務(wù),然而,如何優(yōu)雅地取消正在運行的協(xié)程是一個常見的問題,本文將通過一個具體的例子來展示如何使用 context 包來取消協(xié)程的執(zhí)行,需要的朋友可以參考下
    2024-11-11
  • Go語言中make和new函數(shù)的用法與區(qū)別

    Go語言中make和new函數(shù)的用法與區(qū)別

    這篇文章介紹了Go語言中make和new函數(shù)的用法與區(qū)別,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-07-07
  • 簡單對比一下?C語言?與?Go語言

    簡單對比一下?C語言?與?Go語言

    這篇文章主要介紹了簡單對比一下?C語言?與?Go語言的相關(guān)資料,需要的朋友可以參考下
    2023-08-08
  • go語言中線程池的實現(xiàn)

    go語言中線程池的實現(xiàn)

    Go語言中并沒有直接類似 Java 線程池的內(nèi)建概念,主要通過goroutine和channel來實現(xiàn)并發(fā)處理,本文主要介紹了go語言中線程池的實現(xiàn),具有一定的參考價值,感興趣的可以了解一下
    2025-04-04
  • 淺談用Go構(gòu)建不可變的數(shù)據(jù)結(jié)構(gòu)的方法

    淺談用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-09
  • Go語言高效I/O并發(fā)處理雙緩沖和Exchanger模式實例探索

    Go語言高效I/O并發(fā)處理雙緩沖和Exchanger模式實例探索

    這篇文章主要介紹了Go語言高效I/O并發(fā)處理雙緩沖和Exchanger模式實例探索,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2024-01-01
  • 詳解如何在Golang中監(jiān)聽多個channel

    詳解如何在Golang中監(jiān)聽多個channel

    這篇文章主要為大家詳細介紹了如何在Golang中實現(xiàn)監(jiān)聽多個channel,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-03-03
  • Go?語言入門學(xué)習(xí)之時間包

    Go?語言入門學(xué)習(xí)之時間包

    這篇文章主要介紹了Go?語言入門學(xué)習(xí)之時間包,GO?語言提供了???time??包來測量和顯示時間,下文關(guān)于GO時間包的相關(guān)介紹需要的小伙伴可以參考一下
    2022-04-04
  • Go項目配置管理神器之viper的介紹與使用詳解

    Go項目配置管理神器之viper的介紹與使用詳解

    viper是一個完整的?Go應(yīng)用程序的配置解決方案,它被設(shè)計為在應(yīng)用程序中工作,并能處理所有類型的配置需求和格式,下面這篇文章主要給大家介紹了關(guān)于Go項目配置管理神器之viper的介紹與使用,需要的朋友可以參考下
    2023-02-02
  • Golang httptest包測試使用教程

    Golang httptest包測試使用教程

    這篇文章主要介紹了Golang httptest包測試使用,httptest包的理念是,非常容易模擬http服務(wù),也就是說模擬響應(yīng)寫(response writer),提供給http處理器(handle),讓我們測試http服務(wù)端和客戶端很容易
    2023-03-03

最新評論