转载

设计模式 - 业务代表模式

业务代表模式(Business Delegate Pattern)用于对表示层和业务层解耦。它基本上是用来减少通信或对表示层代码中的业务层代码的远程查询功能。在业务层中我们有以下实体。

  • 客户端(Client) - 表示层代码可以是 JSP、servlet 或 UI java 代码。
  • 业务代表(Business Delegate) - 一个为客户端实体提供的入口类,它提供了对业务服务方法的访问。
  • 查询服务(LookUp Service) - 查找服务对象负责获取相关的业务实现,并提供业务对象对业务代表对象的访问。
  • 业务服务(Business Service) - 业务服务接口。实现了该业务服务的实体类,提供了实际的业务实现逻辑。
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()
}
原文  http://violetzijing.is-programmer.com/posts/215460.html
正文到此结束
Loading...