I have been interested in design patterns for a while and started reading Head First Design Patterns. I started with the first pattern called the 'Strategy' pattern. I went through the problem  outlined in the images below and first tried to propose a solution myself so I could really grasp the importance of the pattern. So my question is that why is my solution to the problem below not good enough. What are the good / bad points of my solution vs the pattern? What makes the pattern clearly the only viable solution?MY SOLUTIONParent Class: DUCK<?phpclass Duck{ public  $swimmable; public  $quackable; public  $flyable; function display() {  echo A Duck Looks Like This<BR/>; } function  quack() {  if($this->quackable==1)  {   echo(Quack<BR/>);  } } function swim() {  if($this->swimmable==1)  {   echo(Swim<BR/>);  } } function  fly() {  if($this->flyable==1)  {   echo(Fly<BR/>);  } }}?>INHERITING CLASS:  MallardDuck<?phpclass MallardDuck extends Duck{ function MallardDuck() {  $this->quackable = 1;  $this->swimmable = 1; } function display() {  echo A Mallard Duck Looks Like This<BR/>; }}?>INHERITING CLASS:  WoddenDecoyDuck<?phpclass WoddenDecoyDuck extends Duck{ function woddendecoyduck() {  $this->quackable = 0;  $this->swimmable = 0; } function display() {  echo A Wooden Decoy Duck Looks Like This<BR/>; }}
Better Understand the 'Strategy' Design Pattern
design patterns
Your code will break when, e.g. ducks quack with a different sounds. The boolean will only lead to other booleans into really hairy if-statements.The strategy pattern is simple though. Take in this instance the quack method and place it into it's own class or interface. An interface in php would be a class that does not contain any implementation other than methods that are stubs (i.e. nothing happens when you call them).class Quackable {    function quack() {}}That way you can create several quackable implementations:class DuckQuack extends Quackable {    function quack() {        return Quack!;    }}class SilentQuack extends Quackable {    function quack() {        return ;             // The decoy duck can't quack. It is silent.    }}And because we did it strategy pattern wise, we can add more types of quackery:class DoubleQuack extends Quackable {    function quack() {        return Quackety-quack!    }}The same can be applied to fly and swim methods for the Duck class. The way you implement a Duck class with a quackable would be like this (the quackable interface is supplied through the constructor, which is sort of how dependency injection works):class Duck {    protected $quackable;    // The constructor    function __construct($quackable) {        $this->quackable = quackable;    }    function quack() {        echo $this->quackable->quack();    }}Implementing the mallard duck and decoy duck will be simple since you only need to supply what kind of quacks the duck should do:class MallardDuck extends Duck {    function __construct() {        parent::__construct(new DuckQuack());           // we construct the mallard duck with a duck quack    }}class DecoyDuck extends Duck {    function __construct() {        parent::__construct(new SilentQuack());           // we construct the decoy duck with a silent quack    }}Using it all is simple:$duck1 = new MallardDuck();$duck2 = new DecoyDuck();$duck1.quack(); // echoes out Quack!$duck2.quack(); // echoes out  (because decoy ducks don't quack)I hope this all makes sense to you.