Alex, 다중 상속이 필요한 대부분의 경우 객체 구조가 다소 올바르지 않다는 신호입니다. 당신이 설명한 상황에서 나는 당신이 단순히 너무 광범위한 계급 책임을 가지고 있음을 알았습니다. 메시지가 애플리케이션 비즈니스 모델의 일부인 경우 출력 렌더링에 신경 쓰지 않아야합니다. 대신 책임을 분할하고 텍스트 또는 html 백엔드를 사용하여 전달 된 메시지를 보내는 MessageDispatcher를 사용할 수 있습니다. 귀하의 코드를 모르지만 다음과 같이 시뮬레이션 해 보겠습니다.
$m = new Message();
$m->type = 'text/html';
$m->from = 'John Doe <jdoe@yahoo.com>';
$m->to = 'Random Hacker <rh@gmail.com>';
$m->subject = 'Invitation email';
$m->importBody('invitation.html');
$d = new MessageDispatcher();
$d->dispatch($m);
이렇게하면 Message 클래스에 몇 가지 전문화를 추가 할 수 있습니다.
$htmlIM = new InvitationHTMLMessage(); // html type, subject and body configuration in constructor
$textIM = new InvitationTextMessage(); // text type, subject and body configuration in constructor
$d = new MessageDispatcher();
$d->dispatch($htmlIM);
$d->dispatch($textIM);
MessageDispatcher는 type전달 된 Message 객체의 속성에 따라 HTML 또는 일반 텍스트로 보낼지 여부를 결정합니다 .
// in MessageDispatcher class
public function dispatch(Message $m) {
if ($m->type == 'text/plain') {
$this->sendAsText($m);
} elseif ($m->type == 'text/html') {
$this->sendAsHTML($m);
} else {
throw new Exception("MIME type {$m->type} not supported");
}
}
요약하자면 책임은 두 클래스로 나뉩니다. 메시지 구성은 InvitationHTMLMessage / InvitationTextMessage 클래스에서 이루어지며 발송 알고리즘은 발송자에게 위임됩니다. 이를 전략 패턴이라고하며 여기에서 자세한 내용을 읽을 수 있습니다 .