您的位置 首页 golang

《Golang学习数据结构和算法》中文版 第5篇

《Learn Data struct ures and Algorithms with Golang》作者: Bhagvan Kommadi

门面( facade )

门面是用来抽象子系统接口的助手。门面模式用于解决接口数量不断增加且系统变得复杂的场景。门面是不同子系统的一个入口点,它简化了系统之间的依赖关系。门面模式提供一个隐藏了代码背后实现的细节接口。

松散耦合原则可以通过门面模式来实现。你可以使用门面来改进设计不佳的API。在 SOA 里,一个服务门面可以合并对契约和实现的改变。

门面模式由facade类,模块类和客户端组成:

  • 门面将来自客户端的请求委托给模块类。facade 隐藏了子系统的逻辑和规则的复杂性。
  • 模块类实现模块子系统的行为和功能。
  • 客户端调用facade方法。facade类功能可以分布在多个包和程序集中。

例如,账户,客户和交易都是具有账户,客户和交易创建方法的类。BranchManagerFacade类被客户端用来创建账户,客户和交易:

 //main package has examples shown
// in Hands-On Data Structures and algorithms with Go book
package main
// importing fmt package
 import  (
  "fmt"
)
//Account struct
type Account struct{
  id string
  accountType string
}
//Account class method create - creates account given AccountType
func (account *Account) create(accountType string) *Account{
  fmt.Println("account creation with type")
  account.accountType = accountType
  return account
}
//Account class method getById given id string
func (account *Account) getById(id string) *Account {
  fmt.Println("getting account by Id")
  return account
}  

account类由deleteById方法,该方法用于删除带有给定ID的账户,如下面的代码所示:

 //Account class method deleteById given id string
func (account *Account) deleteById(id string)() {
  fmt.Println("delete account by id")
}
//Customer struct
type Customer struct{
  name string
  id int
}  

在下面代码中,customer类有一个带name参数的创建新客户方法:

 //Customer class method create - create Customer given name
func (customer *Customer) create(name string) *Customer {
  fmt.Println("creating customer")
  customer.name = name
  return customer
}
// transaction  struct
type Transaction struct{
  id string
  amount float32
  srcAccountId string
  destAccountId string
}  

正如以下代码所示,transaction类有一个create方法来创建一笔交易:

 //Transaction class method create Transaction
func (transaction *Transaction) create(srcAccountId string, destAccountId string,amount float32) *Transaction {
  fmt.Println("creating transaction")
  transaction.srcAccountId = srcAccountId
  transaction.destAccountId = destAccountId
  transaction.amount = amount
  return transaction
}
//BranchManagerFacade struct
type BranchManagerFacade struct {
  account *Account
  customer *Customer
  transaction *Transaction
}
//method NewBranchManagerFacade
func NewBranchManagerFacade() *BranchManagerFacade {
  return &BranchManagerFacade{ &Account{}, &Customer{}, &Transaction{}}
}  

BranchManagerFacade类有createCustomerAccount方法,此方法调用customer实例的create方法,如下代码所示:

 //BranchManagerFacade class method createCustomerAccount
func (facade *BranchManagerFacade) createCustomerAccount(customerName string, accountType string) (*Customer,*Account) {
  var customer = facade.customer.create(customerName)
  var account = facade.account.create(accountType)
  return customer, account
}
//BranchManagerFacade class method createTransaction
func (facade *BranchManagerFacade) createTransaction(srcAccountId string, destAccountId string, amount float32) *Transaction {
  var transaction = facade.transaction.create(srcAccountId,destAccountId,amount)
  return transaction
}  

main方法调用NewBranchManagerFacade方法创建一个门面实例,调用这个facade上的方法可以创建customer和account:

 //main method
func main() {
  var facade = NewBranchManagerFacade()
  var customer *Customer
  var account *Account
  customer, account = facade.createCustomerAccount("Thomas Smith", "Savings")
  fmt.Println(customer.name)
  fmt.Println(account.accountType)
  var transaction = facade.createTransaction("21456","87345",1000)
  fmt.Println(transaction.amount)
}  

运行以下命令:

 go run facade.go  

让我们看看下一节的享元模式。

下一篇:

上一篇:

文章来源:智云一二三科技

文章标题:《Golang学习数据结构和算法》中文版 第5篇

文章地址:https://www.zhihuclub.com/97802.shtml

关于作者: 智云科技

热门文章

网站地图