PHP 디자인 모드 의 장식 모드

본 논문 의 지식 은 라 는 책 에서 기원 되 었 다.
wealthfactor;
	}
}

//      
class DiamondPlains extends Plains{
	function getWealthFactor(){
		return parent::getWealthFactor()+2;
	}
}

//     
class PollutedPlains extends Plains{
	function getWealthFactor(){
		return parent::getWealthFactor()-4;
	}
}

//       ,                    

질문:
오염 되 고 다이아몬드 가 함 유 된 땅 이 나타 나 면 그 부의 가 치 를 계산 해 야 한다. 그러면 우 리 는 여러 가지 종 류 를 만들어 야 하 는가?정 답: 필요 없습니다. 우 리 는 장식 모델 을 이용 하여 기 존의 독립 류 를 조합 하여 하나의 조합 류 가 되 어 부의 가 치 를 계산 하 는 방법 을 약간 바 꿀 수 있 습 니 다.
wealthfactor;
	}
}

//      
class DiamondPlains extends Plains{

	protected $tile;
	function __construct(Tile $tile){
		$this->tile=$tile;
	}

	function getWealthFactor(){
		return parent::getWealthFactor()+2+$this->tile->getWealthFactor();
	}
}

//     
class PollutedPlains extends Plains{

	protected $tile;
	function __construct(Tile $tile){
		$this->tile=$tile;
	}

	function getWealthFactor(){
		return parent::getWealthFactor()-4+$this->tile->getWealthFactor();
	}
}

//     
class PurePlains extends Plains{

	protected $tile;
	function __construct(Tile $tile){
		$this->tile=$tile;
	}

	function getWealthFactor(){
		return parent::getWealthFactor()+4+$this->tile->getWealthFactor();
	}
}


//               
$tile=new DiamondPlains(new PollutedPlains(new PurePlains()));
echo $tile->getWealthFactor();

좋은 웹페이지 즐겨찾기