User Tools

Site Tools


l

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
l [2022/03/09 13:27] chifekl [2023/09/14 06:06] (current) – external edit 127.0.0.1
Line 81: Line 81:
 } }
 </code> </code>
 +
 +
 +//birdRun.php://
 +
 +<code>
 +class BirdRun {
 + 
 +    private $bird; 
 +    public function __construct(Bird $bird) {
 +        $this->bird = $bird;
 +    }
 + 
 +    public function run() {
 +        $flySpeed = $this->bird->fly();
 +    }
 +}
 +</code>
 +
 +//index.php://
 +
 +<code>
 +$bird = new Bird();
 +//$bird = new Duck();
 +//$bird = new Penguin();
 +$birdRun = new BirdRun($bird);
 +$birdRun->run();
 +</code>
 +
 +
 +После замены использования Bird на Duck код будет работать как и прежде - принцип LSP соблюден. 
 +После замены Bird на Penguin код меняет свое поведение, следовательно в данном случае принцип LSP нарушен.
 +
 +Следовать этому типу очень важно при проектировании новых типов с использованием наследования.
 +Этот принцип предупреждает о том, что изменение унаследованного производным типом поведения очень рискованно.
  
  
l.1646832456.txt.gz · Last modified: 2023/09/14 06:06 (external edit)