SoFunction
Updated on 2025-03-05

Golang design pattern exterior pattern explanation and code example

Go AppearancePattern explanation and code examples

AppearanceIt is a structural design pattern that can provide a simple (but limited) interface to complex systems, libraries or frameworks.

Although the appearance mode reduces the overall complexity of the program, it also helps move unwanted dependencies to the same location.

Concept example

It is easy to underestimate the complexity of the work behind the scenes when ordering pizza with a credit card. There will be many subsystems that play a role throughout the process. Here are some of them:

  • Check the account
  • Check the security code
  • Debit/Credit Balance
  • Book entry
  • Send message notification

In such a complex system, it can be said that one step is wrong, which can easily cause big problems. This is why we need appearance patterns, allowing clients to use a simple interface to handle numerous components. The client only needs to enter the card details, security code, payment amount and operation type.

Appearance mode communicates further with multiple components without exposing the internal complexity to the client.

:Appearance

package main
import (
	"fmt"
)
type WalletFacade struct {
	account      *Account
	wallet       *Wallet
	securityCode *SecurityCode
	notification *Notification
	ledger       *Ledger
}
func newWalletFacade(accountID string, code int) *WalletFacade {
	("Starting create account")
	var walletFacade = &WalletFacade{
		account:      newAccount(accountID),
		securityCode: newSecurityCode(code),
		wallet:       newWallet(),
		notification: &Notification{},
		ledger:       &Ledger{},
	}
	("Account created")
	return walletFacade
}
func (w *WalletFacade) addMoneyToWallet(accountID string, securityCode int, amount int) error {
	("Start add money to wallet")
	err := (accountID)
	if err != nil {
		return err
	}
	err = (securityCode)
	if err != nil {
		return err
	}
	(amount)
	()
	(accountID, "credit", amount)
	return nil
}
func (w *WalletFacade) deductMoneyFromWallet(accountID string, securityCode, amount int) error {
	("Starting debit money from wallet")
	err := (accountID)
	if err != nil {
		return err
	}
	err = (securityCode)
	if err != nil {
		return err
	}
	err = (amount)
	if err != nil {
		return err
	}
	()
	(accountID, "debit", amount)
	return nil
}

:Components of complex subsystems

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  != name {
		return ("Account Name is incorrect")
	}
	("Account Verified")
	return nil
}

:Components of complex subsystems

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  != incomingCode {
		return ("Security Code is incorrect")
	}
	("SecurityCode Verified")
	return nil
}

:Components of complex subsystems

package main
import "fmt"
type Wallet struct {
	balance int
}
func newWallet() *Wallet {
	return &Wallet{balance: 0}
}
func (w *Wallet) creditBalance(amount int) {
	 += amount
	("Wallet balance added successfully")
	return
}
func (w *Wallet) debitBalance(amount int) error {
	if  < amount {
		return ("Balance is not sufficient")
	}
	 -= amount
	return nil
}

:Components of complex subsystems

package main
import "fmt"
type Ledger struct{}
func (s *Ledger) makeEntry(accountID, txnType string, amount int) {
	("Make ledger entry for accountId %s with txnType %s for amount %d\n", accountID, txnType, amount)
	return
}

:Components of complex subsystems

package main
import "fmt"
type Notification struct{}
func (n *Notification) sendWalletCreditNotification() {
	("sending wallet credit notification")
}
func (n *Notification) sendWalletDebitNotification() {
	("Sending wallet debit notification")
}

:Client code

package main
import (
	"fmt"
	"log"
)
func main() {
	()
	walletFacade := newWalletFacade("abc", 1234)
	()
	err := ("abc", 1234, 10)
	if err != nil {
		("error: %s \n", ())
	}
	()
	err = ("abc", 1234, 5)
	if err != nil {
		("error: %s \n", ())
	}
}

This is the article about the explanation and code examples of Golang design pattern. For more related Golang appearance pattern content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!