2017-09-14 5 views
0

私はコンテストツリーを生成するプラグインで作業しています。抽象クラスとメソッドでファイルを整理する問題

だから、私は、主に2競技、SingleEliminationの種類、およびPlayoff

インサイドSingleEliminationが、私は、私は選手の2種類を2例、SingleEliminationWithPreliminaryRoundSingleEliminationWithoutPreliminaryRound、各競技の種類の

を持っているを持っていますチーム、競合他社、基本的にチームは競合他社の集まりです。

だから、私は私のコードをこのように整理してみました:

-- TreeGen : (Abstract) All the common code, and the entry point 

---- PlayOffTreeGen (Abstract extends TreeGen) 

------ PlayOffCompetitorTreeGen (extends PlayOffTreeGen) 

------ PlayOffTeamTreeGen (extends PlayOffTreeGen) 

---- SingleEliminationTreeGen (Abstract extends TreeGen) 

------ SingleEliminationTeamTreeGen (extends SingleEliminationTreeGen) 

------ SingleEliminationCompetitorTreeGen (extends SingleEliminationTreeGen) 

ので、この組織は素晴らしい作品、私は条件文の多くを回避し、全体に低複雑性を得ることが、今、私はその方法を持っています例えばSingleEliminationCompetitorTreeGenPlayOffCompetitorTreeGenの両方に複製されます。

私はそれがこの種のアーキテクチャの限界だと思っていますが、どのように進化させるべきか分かりません。

ご了承ください。

答えて

0

多分あなたは形質を使うことができますか?たとえば、DOMDocumentsを使用してHTML(https://github.com/delboy1978uk/form)を生成するフォーム生成ライブラリがあります。

フォーム要素に関係なく、これらのHTMLエンティティはすべて属性セットを持つことができるため、重複したコードで終了しました。私はHasAttributeTraitを作成することによって、それを解決:

namespace Del\Form\Traits; 

trait HasAttributesTrait 
{ 
    /** @var array $attributes */ 
    private $attributes = []; 
    /** 
    * @param $key 
    * @return mixed|string 
    */ 
    public function getAttribute($key) 
    { 
     return isset($this->attributes[$key]) ? $this->attributes[$key] : null; 
    } 
    /** 
    * @param $key 
    * @param $value 
    * @return $this 
    */ 
    public function setAttribute($key, $value) 
    { 
     $this->attributes[$key] = $value; 
     return $this; 
    } 
    /** 
    * @param array $attributes 
    * @return $this 
    */ 
    public function setAttributes(array $attributes) 
    { 
     $this->attributes = $attributes; 
     return $this; 
    } 
    /** 
    * @return array 
    */ 
    public function getAttributes() 
    { 
     return $this->attributes; 
    } 
} 

その後、民間の$属性とゲッターとセッターを持つために使用される任意のクラスで、今ちょうど言う:

use Del\Form\Traits\HasAttributeTrait; 

class Whatever 
{ 
    use HasAtttributesTrait; 

    // other code here 
} 

今、あなたがこれを行うことができます:

$something = new Whatever(); 
$something->setAttribute('href', $url); 

特色はPHP 5.4以降でのみ動作しますが、もちろん最新のものですか? ;-)

関連する問題