Golang设计模式之外观模式讲解和代码示例

 更新时间:2023年06月26日 08:27:25   作者:demo007x  
外观是一种结构型设计模式, 能为复杂系统、 程序库或框架提供一个简单 (但有限) 的接口,这篇文章就给大家详细介绍一下Golang的外观模式,文中有详细的代码示例,具有一定的参考价值,需要的朋友可以参考下

Go 外观模式讲解和代码示例

外观是一种结构型设计模式, 能为复杂系统、 程序库或框架提供一个简单 (但有限) 的接口。

尽管外观模式降低了程序的整体复杂度, 但它同时也有助于将不需要的依赖移动到同一个位置。

概念示例

人们很容易低估使用信用卡订购披萨时幕后工作的复杂程度。 在整个过程中会有不少的子系统发挥作用。 下面是其中的一部分:

  • 检查账户
  • 检查安全码
  • 借记/贷记余额
  • 账簿录入
  • 发送消息通知

在如此复杂的系统中, 可以说是一步错步步错, 很容易就会引发大的问题。 这就是为什么我们需要外观模式, 让客户端可以使用一个简单的接口来处理众多组件。 客户端只需要输入卡片详情、 安全码、 支付金额以及操作类型即可。

外观模式会与多种组件进一步地进行沟通, 而又不会向客户端暴露其内部的复杂性。

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: 复杂子系统的组成部分

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: 复杂子系统的组成部分

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: 复杂子系统的组成部分

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: 复杂子系统的组成部分

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: 复杂子系统的组成部分

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())
	}
}

到此这篇关于Golang设计模式之外观模式讲解和代码示例的文章就介绍到这了,更多相关Golang 外观模式内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 使用Go语言中的Context取消协程执行的操作代码

    使用Go语言中的Context取消协程执行的操作代码

    在 Go 语言中,协程(goroutine)是一种轻量级的线程,非常适合处理并发任务,然而,如何优雅地取消正在运行的协程是一个常见的问题,本文将通过一个具体的例子来展示如何使用 context 包来取消协程的执行,需要的朋友可以参考下
    2024-11-11
  • Go语言中make和new函数的用法与区别

    Go语言中make和new函数的用法与区别

    这篇文章介绍了Go语言中make和new函数的用法与区别,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-07-07
  • 简单对比一下 C语言 与 Go语言

    简单对比一下 C语言 与 Go语言

    这篇文章主要介绍了简单对比一下 C语言 与 Go语言的相关资料,需要的朋友可以参考下
    2023-08-08
  • go语言中线程池的实现

    go语言中线程池的实现

    Go语言中并没有直接类似 Java 线程池的内建概念,主要通过goroutine和channel来实现并发处理,本文主要介绍了go语言中线程池的实现,具有一定的参考价值,感兴趣的可以了解一下
    2025-04-04
  • 浅谈用Go构建不可变的数据结构的方法

    浅谈用Go构建不可变的数据结构的方法

    这篇文章主要介绍了用Go构建不可变的数据结构的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-09-09
  • Go语言高效I/O并发处理双缓冲和Exchanger模式实例探索

    Go语言高效I/O并发处理双缓冲和Exchanger模式实例探索

    这篇文章主要介绍了Go语言高效I/O并发处理双缓冲和Exchanger模式实例探索,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2024-01-01
  • 详解如何在Golang中监听多个channel

    详解如何在Golang中监听多个channel

    这篇文章主要为大家详细介绍了如何在Golang中实现监听多个channel,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2023-03-03
  • Go 语言入门学习之时间包

    Go 语言入门学习之时间包

    这篇文章主要介绍了Go 语言入门学习之时间包,GO 语言提供了 ​​time​​包来测量和显示时间,下文关于GO时间包的相关介绍需要的小伙伴可以参考一下
    2022-04-04
  • Go项目配置管理神器之viper的介绍与使用详解

    Go项目配置管理神器之viper的介绍与使用详解

    viper是一个完整的 Go应用程序的配置解决方案,它被设计为在应用程序中工作,并能处理所有类型的配置需求和格式,下面这篇文章主要给大家介绍了关于Go项目配置管理神器之viper的介绍与使用,需要的朋友可以参考下
    2023-02-02
  • Golang httptest包测试使用教程

    Golang httptest包测试使用教程

    这篇文章主要介绍了Golang httptest包测试使用,httptest包的理念是,非常容易模拟http服务,也就是说模拟响应写(response writer),提供给http处理器(handle),让我们测试http服务端和客户端很容易
    2023-03-03

最新评论