《LearnDataStructuresandAlgorithmswithGolang》作者:BhagvanKommadi 门面(Facade) 门面是用来抽象子系统接口的助手。门面模式用于解决接口数量不断增加且系统变得复杂的场景。门面是不同子系统的一个入口点,它简化了系统之间的依赖关系。门面模式提供一个隐藏了代码背后实现的细节接口。 松散耦合原则可以通过门面模式来实现。你可以使用门面来改进设计不佳的API。在SOA里,一个服务门面可以合并对契约和实现的改变。 门面模式由facade类,模块类和客户端组成:门面将来自客户端的请求委托给模块类。facade隐藏了子系统的逻辑和规则的复杂性。模块类实现模块子系统的行为和功能。客户端调用facade方法。facade类功能可以分布在多个包和程序集中。 例如,账户,客户和交易都是具有账户,客户和交易创建方法的类。BranchManagerFacade类被客户端用来创建账户,客户和交易:mainpackagehasexamplesshowninHandsOnDataStructuresandalgorithmswithGobookpackagemainimportingfmtpackageimport(fmt)AccountstructtypeAccountstruct{idstringaccountTypestring}AccountclassmethodcreatecreatesaccountgivenAccountTypefunc(accountAccount)create(accountTypestring)Account{fmt。Println(accountcreationwithtype)account。accountTypeaccountTypereturnaccount}AccountclassmethodgetByIdgivenidstringfunc(accountAccount)getById(idstring)Account{fmt。Println(gettingaccountbyId)returnaccount} account类由deleteById方法,该方法用于删除带有给定ID的账户,如下面的代码所示:AccountclassmethoddeleteByIdgivenidstringfunc(accountAccount)deleteById(idstring)(){fmt。Println(deleteaccountbyid)}CustomerstructtypeCustomerstruct{namestringidint} 在下面代码中,customer类有一个带name参数的创建新客户方法:CustomerclassmethodcreatecreateCustomergivennamefunc(customerCustomer)create(namestring)Customer{fmt。Println(creatingcustomer)customer。namenamereturncustomer}TransactionstructtypeTransactionstruct{idstringamountfloat32srcAccountIdstringdestAccountIdstring} 正如以下代码所示,transaction类有一个create方法来创建一笔交易:TransactionclassmethodcreateTransactionfunc(transactionTransaction)create(srcAccountIdstring,destAccountIdstring,amountfloat32)Transaction{fmt。Println(creatingtransaction)transaction。srcAccountIdsrcAccountIdtransaction。destAccountIddestAccountIdtransaction。amountamountreturntransaction}BranchManagerFacadestructtypeBranchManagerFacadestruct{accountAccountcustomerCustomertransactionTransaction}methodNewBranchManagerFacadefuncNewBranchManagerFacade()BranchManagerFacade{returnBranchManagerFacade{Account{},Customer{},Transaction{}}} BranchManagerFacade类有createCustomerAccount方法,此方法调用customer实例的create方法,如下代码所示:BranchManagerFacadeclassmethodcreateCustomerAccountfunc(facadeBranchManagerFacade)createCustomerAccount(customerNamestring,accountTypestring)(Customer,Account){varcustomerfacade。customer。create(customerName)varaccountfacade。account。create(accountType)returncustomer,account}BranchManagerFacadeclassmethodcreateTransactionfunc(facadeBranchManagerFacade)createTransaction(srcAccountIdstring,destAccountIdstring,amountfloat32)Transaction{vartransactionfacade。transaction。create(srcAccountId,destAccountId,amount)returntransaction} main方法调用NewBranchManagerFacade方法创建一个门面实例,调用这个facade上的方法可以创建customer和account:mainmethodfuncmain(){varfacadeNewBranchManagerFacade()varcustomerCustomervaraccountAccountcustomer,accountfacade。createCustomerAccount(ThomasSmith,Savings)fmt。Println(customer。name)fmt。Println(account。accountType)vartransactionfacade。createTransaction(21456,87345,1000)fmt。Println(transaction。amount)} 运行以下命令:gorunfacade。go 让我们看看下一节的享元模式。 上一篇:《Golang学习数据结构和算法》中文版第4篇