简体   繁体   English

策略模式无效:我在做什么错?

[英]Strategy pattern not working: What am I doing wrong?

I am a very beginning in PHP and Design Patterns. 我是PHP和设计模式的一个开始。 I have been studying the amazing Head First Design patterns. 我一直在研究惊人的Head First Design模式。 So, I have been trying to translate the original to PHP. 因此,我一直在尝试将原始语言翻译成PHP。 However, I must be doing things very wrong, because it is not working. 但是,我必须做的事情非常错误,因为它不起作用。 Can you help me? 你能帮助我吗?

<?php

interface FlyBehavior{
    public function fly();
}

class FlyWithWings implements FlyBehavior{
    public function fly(){
        echo "I am flying!";
    }
}

class FlyNoWay implements FlyBehavior{
    public function fly(){
        echo "I cannot fly!";
    }
}

interface QuackBehavior{
    public function quack();
}


class Quack implements QuackBehavior{
    public function quack(){
        echo "Quack";
    }
}

class MuteQuack implements QuackBehavior{
    public function quack(){
        echo "Silence";
    }
}

class Squeak implements QuackBehavior{
    public function quack(){
        echo "Squeak";
    }
}

abstract class Duck{
    protected $quackBehavior;
    protected $flyBehavior;

    public function performQuack(){
        $this->$quackBehavior->quack();
    }    

    public function performFly(){
        $this->$flyBehavior->fly();
    }

    abstract public function display();    

    public function swim(){
        echo "All ducks float, even decoy";
    }

}

class MallardDuck extends Duck{
    public function __construct(){
        $this->quackBehavior=new Quack();
        $this->flyBehavior=new FlyWithWings();
    }
    public function display(){
        echo "I am real Mallard duck!";
    }
}

$mallard=new MallardDuck();
$mallard->performQuack();
$mallard->performFly();


?>

When I run I get the error "Undefined variable: quackBehavior" in line "$this->$quackBehavior->quack();" 运行时,在“ $ this-> $ quackBehavior-> quack();”行中出现错误“未定义变量:quackBehavior”。 inside "abstract class Duck". 在“抽象类鸭子”里面。

The error says it all 错误说明了一切

public function performQuack(){
        $this->$quackBehavior->quack();
    }    

    public function performFly(){
        $this->$flyBehavior->fly();
    }

should be 应该

public function performQuack(){
        $this->quackBehavior->quack();
    }    

    public function performFly(){
        $this->flyBehavior->fly();
    }

$this->quackBehaviory refers to a property of your class. $this->quackBehaviory引用您的类的一个属性。

$quackBehaviory means that it's a function scoped variable. $quackBehaviory表示它是一个函数范围的变量。

So to access the class property you need to use $this->quackBehaviory 因此,要访问类属性,您需要使用$this->quackBehaviory

Remove the $ from $this->$quackBehavior->quack(); $this->$quackBehavior->quack();删除$ $this->$quackBehavior->quack(); and $this->$flyBehavior->fly(); $this->$flyBehavior->fly(); ie infront of $quackBehavior and $flyBehavior $quackBehavior$flyBehavior

Should be like this.... 应该是这样的...

 $this->quackBehavior->quack(); //<--- Under your performQuack()

and

 $this->flyBehavior->fly(); //<--- Under your performFly()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM