dongqinta4174 2015-01-09 08:48
浏览 61
已采纳

设计模式策略,可以访问父类

I want to implement the strategy design pattern using php:

interface dummy_function {
    public function render();
}

class a implements dummy_function {
    public function render() {
        echo "class a
";
        // I want to acess x_main::dummy like: echo parent::dummy
    }
}

class b implements dummy_function {
    public function render() {
        echo "class b
";
        // I want to acess x_main::dummy like: echo parent::dummy
    }
}

class x_main {

    public $dummy = "hello world";

    public function setX_Function( dummy_function $funcname ) {
        $this->x_function = $funcname;
    }

    public function render() {
        $this->x_function->render();
    }
}


$test = new x_main();
$test->setX_Function( new a() );
$test->render();

Inside my classes I want to access to some methods and variables defined in the main class. Unfortunatelly "parent" does not work to access contents from the class "x_main" inside the implementation classes.

A way i found is to give $this as parameter to the method "render", like:

class x_main {
    [...] 
    public function render() {
        $this->x_function->render( $this );
    }
}

class a implements dummy_function {
    public function render( $mainclass ) {
        echo "class a
";
        echo $mainclass->dummy;
    }
}

The next way i testest is to set the variable from the class main direcly into the implemented function, like:

class x_main {

    [ ... ]     
    public function setX_Function( dummy_function $funcname ) {
        $this->x_function = $funcname;
        $this->x_function->dummy = $dummy;
    }

}

class a implements dummy_function {
    public function render() {
        echo "class a
";
        echo $this->dummy;
    }
}

Both solutions work, but I feel a bit confused if that's the best way to implement my desired idea. It looks extremly like a workaround but not like a real OO-programming.

I'm looking forward for your ideas.

  • 写回答

1条回答 默认 最新

  • dsadasd12132 2015-01-09 09:42
    关注

    Rehashing the comments above:

    The two classes aren't related in any way, they certainly don't have a parent relationship. One object holding an instance of another object does not mean these two objects are in any sort of relationship with one another. You simply need to explicitly pass data into your dummy_function instance; e.g.: public function render(array $data).

    Response:

    In my first solution I put the whole mainclass as parameter to the render function. So this is a solution that will work in any case. But if there is definitivly no relationship between these two objects I prefer my second solution by setting the parameters directly with $this->x_function->dummy = $dummy;

    Sorry to tell you that you're wrong there. Implicitly setting a property on an object is in no way a defined interface. And I'm not using this word in the sense of PHP's interface keyword, I mean this in the broader sense of specifying how two pieces of code can interact and cooperate.

    You've done a good job of specifying and using an interface for the render() function, this greatly decreases code coupling and increases flexibility and testability. Now you're destroying this again by using undefined and brittle property assignments.

    You need to make the passing data into render aspect part of your interface specification. For example:

    interface dummy_function {
        public function render(array $data);
    }
    

    Or:

    interface dummy_function {
        public function setData(array $data);
        public function render();
    }
    

    Or better:

    interface DummyData {
    
        /**
         * @return string
         */
        public function getDummyData();
    
    }
    
    interface DummyRenderer {
    
        public function render(DummyData $data);
    
    }
    

    With this you're:

    • explicitly specifying what format your data is in (string)
    • codifying where render() can get access to such data (it'll receive a DummyData object which has a getDummyData() method)

    You don't need to guess what property names are involved or what structure the passed object has.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置