业务代表模式
业务代表模式(Business Delegate Pattern)用于对表示层和业务层解耦。它基本上是用来减少通信或对表示层代码中的业务层代码的远程查询功能。在业务层中我们有以下实体。
- 客户端(Client) - 表示层代码可以是 JSP、servlet 或 UI java 代码。
- 业务代表(Business Delegate) - 一个为客户端实体提供的入口类,它提供了对业务服务方法的访问。
- 查询服务(LookUp Service) - 查找服务对象负责获取相关的业务实现,并提供业务对象对业务代表对象的访问。
- 业务服务(Business Service) - 业务服务接口。实现了该业务服务的实体类,提供了实际的业务实现逻辑。
实现
我们将创建 Client、BusinessDelegate、BusinessService、LookUpService、JMSService 和 EJBService 来表示业务代表模式中的各种实体。
BusinessDelegatePatternDemo,我们的演示类使用 BusinessDelegate 和 Client 来演示业务代表模式的用法。
源码
#include <string.h>
#include <iostream>
#include <string>
namespace BusinessDelegatePattern {
class BusinessService {
public:
virtual void doProcessing() = 0;
};// class BusinessService
class EJBService : public BusinessService {
public:
void doProcessing() override {
std::cout << "EJBService" << std::endl;
}
};// class EJBService
class JMSService : public BusinessService {
public:
void doProcessing() override {
std::cout << "JMSService" << std::endl;
}
};// class JMSService
class BusinessLookUp {
public:
std::shared_ptr<BusinessService> getBusinessService(const std::string &serviceType) {
if (::strcasecmp("EJB", serviceType.data()) == 0) {
return std::make_shared<EJBService>();
} else if (::strcasecmp("JMS", serviceType.data()) == 0) {
return std::make_shared<JMSService>();
} else {
return nullptr;
}
}
};// class BusinessLookUp
class BusinessDelegate {
public:
BusinessDelegate() {
lookupService_ = std::make_shared<BusinessLookUp>();
}
void setServiceType(const std::string &serviceType){
serviceType_ = serviceType;
}
void doTask(){
businessService_ = lookupService_->getBusinessService(serviceType_);
businessService_->doProcessing();
}
private:
std::shared_ptr<BusinessService> businessService_;
std::string serviceType_;
std::shared_ptr<BusinessLookUp> lookupService_;
};// class BusinessDelegate
class Client {
public:
Client(const BusinessDelegate &businessDelegate){
businessDelegate_ = businessDelegate;
}
void doTask() {
businessDelegate_.doTask();
}
private:
BusinessDelegate businessDelegate_;
};// class Client
}// namespace business_delegate_pattern
int main() {
BusinessDelegatePattern::BusinessDelegate businessDelegate;
businessDelegate.setServiceType("EJB");
BusinessDelegatePattern::Client client(businessDelegate);
client.doTask();
return 0;
}