2014-12-09 2 views
0

전체 텍스트 검색 서비스에 사용할 최상의 디자인 패턴을 결정하려고합니다. 내가 상상하는 기본 논리는 아래에 코딩되어 있습니다. 또한 요지가 있습니다 : https://gist.github.com/qutwala/1b574a39286a7525c8c7 search() 메소드를 호출하면 구성에 따라 검색 엔진 (Sphinx, Solr 등)과 구현 (Local, API)을 결정해야합니다. 로드 할 엔진.디자인 패턴을 사용하는 구현 시스템을 사용하는 여러 공급자

정확히 어떤 디자인 패턴을 사용해야할지 모르겠습니다. 처음에는 나는 거기에 참여할 수있을 것 같아 공장, 싱글 톤 디자인 패턴.

  • 어떻게 설정할 때 분사 특정 구현에 모든 속성 클래스 : 내 현재의 구현으로

    나는 다음과 같은 문제를 참조하십시오.

  • 특정 구현에서 속성에 액세스하는 방법.
  • 그리고 나는 그것이 자극적이지 않을 때까지 더 많은 것들이있을 것이라고 생각합니다. :)

앞서 언급했듯이 패턴을 사용하는 올바른 방법에 관심이 있으니 생각을 환영합니다.

코드 :

<?php 

Interface SearchEngine 
{ 
    public function getSearchProperties(); 

    public function setSearchProperties(SearchProperties $properties); 

    public function getImplementation($implementation); 
} 

Interface SearchPropertiesInterface 
{ 
    public function setIndex($index); 
} 

class SearchProperties implements SearchPropertiesInterface 
{ 
    private $properties = array(); 

    public function setProperty($property, $value) { 
     echo "Set property <br />"; 
     $this->properties[$property] = $value; 
    } 

    public function setIndex($index) { 
     $this->setProperty('index', $index); 
    } 
} 

class SphinxSearchEngine implements SearchEngine 
{ 
    private $SearchProperties; 

    public function __construct() 
    { 
     $this->SearchProperties = new SearchProperties(); 
    } 

    private $implementations = array(); 

    /** 
    * @param $implementation 
    * 
    * @return SphinxSearchImplementation 
    */ 
    public function getImplementation($implementation) 
    { 
     echo "Returned SphinxSearchEngineAPIImplementation <br />"; 
     // TODO: on demand load a.ka. singleton? 
     $this->implementations[$implementation] = new SphinxSearchEngineAPIImplementation(); 

     return $this->implementations[$implementation]; 
    } 

    /** 
    * TODO: how to inject properties? 
    * TODO: what if changed implementation, should be injected in both? 
    * 
    * @return SearchProperties 
    */ 
    public function getSearchProperties() 
    { 
     return $this->SearchProperties; 
    } 

    public function setSearchProperties(SearchProperties $properties) 
    { 

    } 
} 

Interface SphinxSearchImplementation 
{ 
    public function search($query, $limit, $offset); 
} 

class SphinxSearchEngineAPIImplementation implements SphinxSearchImplementation 
{ 
    public function search($query, $limit, $offset) { 
     //TODO: how to get properties, index? 
     echo "Find results by SphinxSearchEngineAPIImplementation <br />"; 
    } 
} 

class ElasticSearchEngine implements SearchEngine 
{ 
    private $implementations = array(); 

    public function __construct() 
    { 
     $this->SearchProperties = new SearchProperties(); 
    } 

    /** 
    * @param $implementation 
    * 
    * @return SphinxSearchImplementation 
    */ 
    public function getImplementation($implementation) 
    { 
     return $this->implementations[$implementation]; 
    } 

    public function getSearchProperties() 
    { 
     return $this->SearchProperties; 
    } 

    public function setSearchProperties(SearchProperties $properties) 
    { 
     // TODO: how to inject properties? 
     // TODO: what if changed implementation, should be injected in both? 
    } 
} 

class Search 
{ 
    public function __construct() { 
     // magic goes here :) 
    } 

    private $config = array(
     "engine" => "Sphinx|ElasticSearch" 
    ); 

    public function getEngine() 
    { 
     echo "Loaded engine: Sphinx <br />"; 
     return new SphinxSearchEngine(); // For example by config loaded sphinx SE? 
    } 

    public function search($query, $limit, $offset) { 
     // TODO: how to decide which engine to use? 
     // TODO: engine should have own properties set? 

     $this->getEngine()->getSearchProperties()->setIndex('search_index'); 

     try { 
      $this->getEngine()->getImplementation('local|api')->search($query, $limit, $offset); 
     } catch (Exception $e) { 
      // TODO: What to do now, fallback to another engine? 
     } 
    } 
} 

$search = new Search(); 
$search->search('my search query', 10, 0); 

답변

0

내 생각, 전략 패턴은 당신이 찾고있는 무엇.
그래서 당신은 가지고있는 플래그 사이를 전환하는 팩토리 클래스를 갖게 될 것입니다. 다음과 같은
뭔가 :

class Factory() 
{ 
    public Factory CreateFactory(Enum parameter) 
    { 
     Factory manager = null; 
     switch(parameter) 
     { 
      case 1: 
       manager = new ImplementationOne(); 
       break; 
      case 2: 
       manager new ImplementationTwo(); 
       break; 
      else: 
       break; 
     }   
     return manager; 
    }  
} 

그런 다음 등등 평소와 같이 ImplementationOne 클래스와이있을 수 있습니다.

참조 : Strategy Pattern

, 당신을 감사

관련 문제