<?PHP//interface, Ducks fly in several different ways of interfacingInterfaceflybehavior{ Public functionfly ();}//Way OneclassFlywithwingsImplementsflybehavior{ Public functionFly () {Echo"Fly with Wings \ n"; }}//Mode 2classFlywithnoImplementsflybehavior{ Public functionFly () {Echo"Fly with No Wings \ n"; }}//Duck class//1, Record flight mode//2, make call flight function intermediate Bridgeclassduck{Private $_flybehavior; Public functionPerformfly () {$this->_flybehavior->fly ();//2, do call the flight function intermediate Bridge } Public functionSetflybehavior (Flybehavior$behavior) { $this->_flybehavior =$behavior;//1. Record flight mode }} classRubberduckextendsduck{}//Test Case$duck=NewRubberduck ();/*want the Ducks to fly with their wings*/$duck->setflybehavior (Newflywithwings ());$duck-Performfly (); /*trying to make ducks fly without wings .*/$duck->setflybehavior (NewFlywithno ());$duck->performfly ();
Strategy mode-How ducks fly-examples