最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

Go 外觀模式講解和代碼示例

外觀是一種結(jié)構(gòu)型設(shè)計(jì)模式, 能為復(fù)雜系統(tǒng)、 程序庫(kù)或框架提供一個(gè)簡(jiǎn)單 (但有限) 的接口。

盡管外觀模式降低了程序的整體復(fù)雜度, 但它同時(shí)也有助于將不需要的依賴(lài)移動(dòng)到同一個(gè)位置。

概念示例

人們很容易低估使用信用卡訂購(gòu)披薩時(shí)幕后工作的復(fù)雜程度。 在整個(gè)過(guò)程中會(huì)有不少的子系統(tǒng)發(fā)揮作用。 下面是其中的一部分:

  • 檢查賬戶(hù)
  • 檢查安全碼
  • 借記/貸記余額
  • 賬簿錄入
  • 發(fā)送消息通知

在如此復(fù)雜的系統(tǒng)中, 可以說(shuō)是一步錯(cuò)步步錯(cuò), 很容易就會(huì)引發(fā)大的問(wèn)題。 這就是為什么我們需要外觀模式, 讓客戶(hù)端可以使用一個(gè)簡(jiǎn)單的接口來(lái)處理眾多組件。 客戶(hù)端只需要輸入卡片詳情、 安全碼、 支付金額以及操作類(lèi)型即可。

外觀模式會(huì)與多種組件進(jìn)一步地進(jìn)行溝通, 而又不會(huì)向客戶(hù)端暴露其內(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: 客戶(hù)端代碼

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è)計(jì)模式之外觀模式講解和代碼示例的文章就介紹到這了,更多相關(guān)Golang 外觀模式內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Go語(yǔ)言程序查看和診斷工具詳解

    Go語(yǔ)言程序查看和診斷工具詳解

    這篇文章主要為大家詳細(xì)介紹了Go語(yǔ)言程序查看和診斷工具,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • golang并發(fā)編程的實(shí)現(xiàn)

    golang并發(fā)編程的實(shí)現(xiàn)

    這篇文章主要介紹了golang并發(fā)編程的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-01-01
  • Go語(yǔ)言中日志管理詳解之從log到zap

    Go語(yǔ)言中日志管理詳解之從log到zap

    在軟件開(kāi)發(fā)的世界里,日志就像是應(yīng)用程序的黑匣子,記錄著程序運(yùn)行過(guò)程中的關(guān)鍵信息,幫助開(kāi)發(fā)者在遇到問(wèn)題時(shí)快速定位和解決,在Go語(yǔ)言生態(tài)中,日志管理有著豐富的工具和方案,這篇文章主要介紹了Go語(yǔ)言中日志管理詳解之從log到zap的相關(guān)資料,需要的朋友可以參考下
    2026-04-04
  • Go語(yǔ)言Goroutines?泄漏場(chǎng)景與防治解決分析

    Go語(yǔ)言Goroutines?泄漏場(chǎng)景與防治解決分析

    這篇文章主要為大家介紹了Go語(yǔ)言Goroutines?泄漏場(chǎng)景與防治解決分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • Golang報(bào)“import cycle not allowed”錯(cuò)誤的2種解決方法

    Golang報(bào)“import cycle not allowed”錯(cuò)誤的2種解決方法

    這篇文章主要給大家介紹了關(guān)于Golang報(bào)"import cycle not allowed"錯(cuò)誤的2種解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以們下面隨著小編來(lái)一起看看吧
    2018-08-08
  • GoLang之go build命令的具體使用

    GoLang之go build命令的具體使用

    本文主要介紹了GoLang之go build命令的具體使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • Golang Mutex實(shí)現(xiàn)互斥的具體方法

    Golang Mutex實(shí)現(xiàn)互斥的具體方法

    Mutex是Golang常見(jiàn)的并發(fā)原語(yǔ),在開(kāi)發(fā)過(guò)程中經(jīng)常使用到,本文主要介紹了Golang Mutex實(shí)現(xiàn)互斥的具體方法,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-04-04
  • 聊聊golang中多個(gè)defer的執(zhí)行順序

    聊聊golang中多個(gè)defer的執(zhí)行順序

    這篇文章主要介紹了golang中多個(gè)defer的執(zhí)行順序,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-05-05
  • 用Go獲取短信驗(yàn)證碼的示例代碼

    用Go獲取短信驗(yàn)證碼的示例代碼

    要用Go獲取短信驗(yàn)證碼,通常需要連接到一個(gè)短信服務(wù)提供商的API,并通過(guò)該API發(fā)送請(qǐng)求來(lái)獲取驗(yàn)證碼,由于不同的短信服務(wù)提供商可能具有不同的API和授權(quán)方式,我將以一個(gè)簡(jiǎn)單的示例介紹如何使用Go語(yǔ)言來(lái)獲取短信驗(yàn)證碼,需要的朋友可以參考下
    2023-07-07
  • golang語(yǔ)言如何將interface轉(zhuǎn)為int, string,slice,struct等類(lèi)型

    golang語(yǔ)言如何將interface轉(zhuǎn)為int, string,slice,struct等類(lèi)型

    這篇文章主要介紹了golang語(yǔ)言如何將interface轉(zhuǎn)為int, string,slice,struct等類(lèi)型,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-12-12

最新評(píng)論

眉山市| 贵阳市| 吉首市| 佳木斯市| 景德镇市| 舞阳县| 开平市| 黔西县| 军事| 盘锦市| 积石山| 丰台区| 咸阳市| 沂南县| 宁国市| 会泽县| 潞城市| 宽城| 永顺县| 甘德县| 龙海市| 沙坪坝区| 开化县| 香格里拉县| 遵义市| 碌曲县| 清丰县| 忻城县| 瓦房店市| 商水县| 忻城县| 井研县| 贞丰县| 夏河县| 嘉峪关市| 崇文区| 宣武区| 白沙| 和平区| 邯郸县| 靖州|