GoLang设计模式24 - 门面模式(结构型)
门面模式是一种结构型设计模式。这种模式的作用是隐藏系统底层的复杂逻辑,只提供一个简单易用的接口给调用者。通过门面模式,将系统底层所需要的各种接口整理为一个通用的接口以便于调用方使用。换句话说,就是门面模式为一个复杂系统做了更高层次的抽象。
门面这个词的释义为:
指商店房屋及沿街的部分,指店铺外表
以上来自百度百科。
正如这个词的表面意义:对于一个建筑,一般的路人只能看到它的外表,而不会知道这个建筑内里的复杂结构。
下面通过一个简单的例子来了解门面模式:在移动互联网时代,人们在付款时通常都会选在在线支付的方式。就在在线的这样一个简单操作中,支付系统底层做了许多的动作。但是不管是付款方还是收款方对此都没有感知。
下面是收付款过程中,电子支付系统需要做的几件事情:
- 账户检查(Check Account)
- 安全校验(Check Security Pin)
- 收支检验(Credit/Debit Balance)
- 账目记录(Make Ledger Entry)
- 发送提醒(Send Notification)
正如上面所列出的,在一个简单付款操作背后隐藏了许多操作。这就是门面模式可以发挥作用的地方。作为一个使用者,只需要输入账户、密码、金额等几项基础信息,其它的操作都可以交给系统在后台自动完成。

综上所述,我们可以知道门面模式的使用场景:当我们想为一个复杂系统提供简单调用方式的时候就可以使用门面模式。
门面模式的UML类图如下:

下面是在线支付这个案例对应的UML类图:

看下在线支付这个例子的代码实现:
walletFacade.go
1 type walletFacade struct { 2 account *account 3 wallet *wallet 4 securityCode *securityCode 5 notification *notification 6 ledger *ledger 7 } 8 9 func newWalletFacade(accountID string, code int) *walletFacade { 10 fmt.Println("Starting create account") 11 walletFacade := &walletFacade{ 12 account: newAccount(accountID), 13 securityCode: newSecurityCode(code), 14 wallet: newWallet(), 15 notification: ¬ification{}, 16 ledger: &ledger{}, 17 } 18 fmt.Println("Account created") 19 return walletFacade 20 } 21 22 func (w *walletFacade) addMoneyToWallet(accountID string, securityCode int, amount int) error { 23 fmt.Println("Starting add money to wallet") 24 err := w.account.checkAccount(accountID) 25 if err != nil { 26 return err 27 } 28 err = w.securityCode.checkCode(securityCode) 29 if err != nil { 30 return err 31 } 32 w.wallet.creditBalance(amount) 33 w.notification.sendWalletCreditNotification() 34 w.ledger.makeEntry(accountID, "credit", amount) 35 return nil 36 } 37 38 func (w *walletFacade) deductMoneyFromWallet(accountID string, securityCode int, amount int) error { 39 fmt.Println("Starting debit money from wallet") 40 err := w.account.checkAccount(accountID) 41 if err != nil { 42 return err 43 } 44 err = w.securityCode.checkCode(securityCode) 45 if err != nil { 46 return err 47 } 48 err = w.wallet.debitBalance(amount) 49 if err != nil { 50 return err 51 } 52 w.notification.sendWalletDebitNotification() 53 w.ledger.makeEntry(accountID, "credit", amount) 54 return nil 55 }
account.go
1 type account struct { 2 name string 3 } 4 5 func newAccount(accountName string) *account { 6 return &account{ 7 name: accountName, 8 } 9 } 10 11 func (a *account) checkAccount(accountName string) error { 12 if a.name != accountName { 13 return fmt.Errorf("Account Name is incorrect") 14 } 15 fmt.Println("Account Verified") 16 return nil 17 }
securityCode.go
1 type securityCode struct { 2 code int 3 } 4 5 func newSecurityCode(code int) *securityCode { 6 return &securityCode{ 7 code: code, 8 } 9 } 10 11 func (s *securityCode) checkCode(incomingCode int) error { 12 if s.code != incomingCode { 13 return fmt.Errorf("Security Code is incorrect") 14 } 15 fmt.Println("SecurityCode Verified") 16 return nil 17 }
wallet.go
1 type wallet struct { 2 balance int 3 } 4 5 func newWallet() *wallet { 6 return &wallet{ 7 balance: 0, 8 } 9 } 10 11 func (w *wallet) creditBalance(amount int) { 12 w.balance += amount 13 fmt.Println("Wallet balance added successfully") 14 return 15 } 16 17 func (w *wallet) debitBalance(amount int) error { 18 if w.balance < amount { 19 return fmt.Errorf("Balance is not sufficient") 20 } 21 fmt.Println("Wallet balance is Sufficient") 22 w.balance = w.balance - amount 23 return nil 24 }
ledger.go
1 type ledger struct { 2 } 3 4 func (s *ledger) makeEntry(accountID, txnType string, amount int) { 5 fmt.Printf("Make ledger entry for accountId %s with txnType %s for amount %d", accountID, txnType, amount) 6 return 7 }
notification.go
1 type notification struct { 2 } 3 4 func (n *notification) sendWalletCreditNotification() { 5 fmt.Println("Sending wallet credit notification") 6 } 7 8 func (n *notification) sendWalletDebitNotification() { 9 fmt.Println("Sending wallet debit notification") 10 }
main.go
1 func main() { 2 fmt.Println() 3 walletFacade := newWalletFacade("abc", 1234) 4 fmt.Println() 5 err := walletFacade.addMoneyToWallet("abc", 1234, 10) 6 if err != nil { 7 log.Fatalf("Error: %s\n", err.Error()) 8 } 9 fmt.Println() 10 err = walletFacade.deductMoneyFromWallet("ab", 1234, 5) 11 if err != nil { 12 log.Fatalf("Error: %s\n", err.Error()) 13 } 14 }
输出内容:
1 Starting create account 2 Account created 3 4 Starting add money to wallet 5 Account Verified 6 SecurityCode Verified 7 Wallet balance added successfully 8 Sending wallet credit notification 9 Make ledger entry for accountId abc with txnType credit for amount 10 10 Starting debit money from wallet 11 2022/03/13 21:29:00 Error: Account Name is incorrect
浙公网安备 33010602011771号