美文网首页
业务代表模式

业务代表模式

作者: 散装咖啡 | 来源:发表于2017-05-30 02:46 被阅读62次
    //businessDelegate
    interface BusinessService
    {
        public function doProcessing();
    }
    
    class EJBService implements BusinessService
    {
        public function doProcessing()
        {
            echo 'Processing task by invoking EJB Service' . "<br />";
        }
    }
    
    
    class JMSService implements BusinessService
    {
        public function doProcessing()
        {
            echo 'Processing task by invoking JMS Service'. "<br />";
        }
    }
    
    class BusinessLookUp
    {
        public function getBusinessService($serviceType)
        {
            //返回0就是相等
            if (!strcasecmp('EJB', $serviceType)) {
                return new EJBService();
            } else {
                return new JMSService();
            }
        }
    }
    
    class BusinessDelegate
    {
        private $lookupService = null;
        private $businessService = null;
        private $serviceType = null;
        
        public function __construct()
        {
            $this->lookupService = new BusinessLookUp;
        }
        
        public function setServiceType($serviceType)
        {
            $this->serviceType = $serviceType;
        }
        
        public function doTask()
        {
            $this->businessService = $this->lookupService->getBusinessService($this->serviceType);
            $this->businessService->doProcessing();
        }
    }
    
    class Client
    {
        public $businessService = null;
        
        public function Client($businessService)
        {
            $this->businessService = $businessService;
        }
        
        public function doTask()
        {
            $this->businessService->doTask();
        }
    }
    
    
    $BusinessDelegate = new BusinessDelegate();
    $BusinessDelegate->setServiceType("EJB");
    
    $Client = new Client($BusinessDelegate);
    $Client->doTask();
    
    $BusinessDelegate->setServiceType("JMS");
    $Client->doTask();
    

    参考文章 http://www.runoob.com/design-pattern/business-delegate-pattern.html

    相关文章

      网友评论

          本文标题:业务代表模式

          本文链接:https://www.haomeiwen.com/subject/flglfxtx.html