范文健康探索娱乐情感热点
投稿投诉
热点动态
科技财经
情感日志
励志美文
娱乐时尚
游戏搞笑
探索旅游
历史星座
健康养生
美丽育儿
范文作文
教案论文
国学影视

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

  《Learn Data Structures 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学习数据结构和算法》中文版 第4篇

华为P60Pro曝光,是大家期待的5G新机备受期待的年度旗舰华为P50系列已经发布,虽然被不少网友吐槽没有5G算不上高端旗舰,但是通过各大电商平台的预售数据来看,果然还是真香了。近日外媒又曝光了一组华为P60Pro的渲染图华为P50Pro与Mate40Pro,谁更香?华为P50Pro发布已经有一周多的时间,想必大家对于计算光学原色引擎的实际表现充满好奇!3。5倍长焦到底够不够用?暗光拍摄华为又有哪些新技术?它跟老大哥华为Mate40Pro相比谁C语言ampampampJava解析当ii后,结果究竟是什么?前言之前有朋友跟我讨论过一个问题,他问在java中,i1ii之后的结果是什么,我一开始认为是2,但当他说答案是1的时候,我不得不想一个合理的解释,我想,可能是因为临时变量,当i赋值微信最近有哪些新变化?医用口罩来了,实用功能边写边译灰测今日节目音频微信最近有版本更新吗?有,微信8。0。10foriOS三天前更新,功能介绍依旧是熟悉的本次更新解决了一些已知问题。虽然语焉不详,但微信最近的确有不少小更新,从表情到灰测2021年Q2中国智能手机市场vivo第一,荣耀重回前五前不久,国际数据公司IDC发布了新一轮智能手机跟踪报告,报告显示了2021年第二季度,中国智能手机市场排名前五手机厂商的生产表现数据。其中,vivo斩获国内第一,2021年第二季度阿里女员工性侵案最新进展出炉,马云隐退后,阿里管理出了问题?职场是每个成年人都必须要经历的阶段,有些人在不断地求职过程中,抓住了属于自己的机会,从而一举走上人生巅峰。就比如说阿里巴巴的创始人马云,他最开始只是一名英语老师,如果不是因为创办了2021年,阿里巴巴灌醉性侵女下属的事件你怎么看?这件事很重要这几天除了奥运会和新冠疫情,这个新闻估计是最大的瓜了,首先我们来梳理一下事情发生的经过。7月27日晚,曲某等人灌醉性侵女下属。7月28日早,女员工报警,曲某被传讯。8月冲上热搜!微信最新变化作为一款国民级App,微信软件每一次升级调整都引发万千网友的关注。不知道大家最近有没有发现,微信里的一个表情悄悄更新赶快查看手机微信之后,发现生病这个表情确实是戴上了医用口罩,之前88购物节期间,沃尔玛京东到家平台销售额连创新高8月10日,资本邦了解到,8月9日,沃尔玛(WMT。US)公布88购物节至今战报。88购物节是由京东和沃尔玛联手打造的大型促销活动,具体时间为7月15日8月11日,今年是其第五周年漠视女员工被侵害,阿里巴巴该审视的是企业文化一场丑闻,互联网大厂阿里巴巴的反应迟钝甚至冷漠成了众矢之的。阿里女员工被侵害事件,根据自述,她按领导要求陪公司客户喝酒,却被灌醉后遭到猥亵。此后,领导开了房间进出房间4次,醉酒状态阿里公司在此次事件中,应该积极面对,而不是避重就轻这俩天,又有一个互联网公司上了热搜啊,就是那个宣称9大福报的某巴巴公司,啊,这不是福报公司嘛,怎么会出事情呢,这可是中国互联网企业的领头羊嘛,这可是996工人的福报公司啊。害不敢相