业务代表模式(Business Delegate Pattern)用于对表示层和业务层解耦。它基本上是用来减少通信或对表示层代码中的业务层代码的远程查询功能。在业务层中我们有以下实体。
package main import "fmt" type BusinessService interface { DoProcessing() } type EJBService struct{} func (e *EJBService) DoProcessing() { fmt.Println("Processing task by invoking EJB service") } type JMSService struct{} func (j *JMSService) DoProcessing() { fmt.Println("Processing task by invoking JMS service") } type BusinessLookUp struct{} func (b *BusinessLookUp) GetBusniessService(serviceType string) BusinessService { if serviceType == "EJB" { return &EJBService{} } return &JMSService{} } type BusinessDelegate struct { LookUpService *BusinessLookUp BusinessService BusinessService ServiceType string } func (b *BusinessDelegate) SetServiceType(serviceType string) { b.ServiceType = serviceType } func (b *BusinessDelegate) DoProcessing() { service := b.LookUpService.GetBusniessService(b.ServiceType) service.DoProcessing() } type Client struct { Service BusinessService } func NewClient(service BusinessService) *Client { return &Client{ Service: service, } } func (c *Client) DoTask() { c.Service.DoProcessing() } func main() { delegate := &BusinessDelegate{LookUpService: &BusinessLookUp{}} delegate.SetServiceType("EJB") client := NewClient(delegate) client.DoTask() delegate.SetServiceType("JMS") client.DoTask() }