装饰图案与方法关系



我在PHP中使用了设计模式Decorator,但我遇到了结构问题。

这里有一个简单的例子来说明我的问题:

interface MyInterface {
function showUsers($array);
function showUser($i);
}
class MyCLass implements MyInterface {
function showUsers($array)
{
foreach($array as $toto) {
$this->showUser($toto);
}
}
function showUser($i)
{
echo $i;
}
}
class MyCLassDecorator implements MyInterface {
private $inner;
public function __construct(MyInterface $inner)
{
$this->inner = $inner;
}
function showUsers($array)
{
$this->inner->showUsers($array);
}
function showUser($i)
{
echo "User: $i";
}
}
class MyCLassDecorator2 implements MyInterface {
private $inner;
public function __construct(MyInterface $inner)
{
$this->inner = $inner;
}
function showUsers($array)
{
$this->inner->showUsers($array);
}
function showUser($i)
{
$this->inner->showUser($i);
echo " is wonderful";
}
}
$myClass = new MyCLassDecorator2(new MyCLassDecorator(new MyCLass()));
$myClass->showUsers(["Alfred", "Bob", "Claire"]);

有了这段代码,方法显示MyClassDecorator&MyClassDecorator2永远不会被调用。

我能做什么?是否禁止调用同一类的另一个方法?(拆分代码不是很方便)有别的办法吗?我应该通过方法创建一个接口吗?

非常感谢:)

编辑:

这是我最终使用的解决方案,尽管我对它并不满意…

我不是在方法中分割代码,而是在接口(服务)中分割代码

这是:

interface IShowUsers {
function showUsers($array);
}
interface IShowUser {
function showUser($user);
}
class Services {
static $showUsers;
static $showUser;
}
class MyShowUsers implements IShowUsers {
function showUsers($array)
{
foreach($array as $toto) {
Services::$showUser->showUser($toto);
}
}
}
class MyShowUser implements IShowUser {
function showUser($user)
{
echo $user;
}
}
class MyShowUserDecorator implements IShowUser {
private $inner;
public function __construct(IShowUser $inner)
{
$this->inner = $inner;
}
function showUser($user)
{
echo "User: ";
$this->inner->showUser($user)
}
}
class MyShowUserDecorator2 implements IShowUser {
private $inner;
public function __construct(MyInterface $inner)
{
$this->inner = $inner;
}
function showUser($user)
{
$this->inner->showUser($user);
echo " is wonderful";
}
}
$myClass = new MyShowUserDecorator2(new MyShowUserDecorator(new MyShowUser()));
Services::$showUsers = new MyShosUsers();
Services::$showUser = new MyShowUserDecorator2(new MyShowUserDecorator(new MyShowUser()));
Services::$showUsers->showUsers(["Alfred", "Bob", "Claire"]);

如果你有更好的解决方案,我会很高兴知道:)

当然,在许多项目中,我使用decorator模式以不同的方式使用这些decorator,例如以下示例:

//no decorators
Services::$showUser = new MyShowUser();
//only the first
Services::$showUser = new MyShowUserDecorator(new MyShowUser());
//only the second
Services::$showUser = new MyShowUserDecorator2(new MyShowUser());

因此,延期似乎不是一个好的解决方案。

再次感谢您提供正确的方法:)

在我看来,你需要重新思考一下。如果你能清楚地了解你正在努力实现的目标,那么我们就能提供更多的见解。但要直接回答你的问题,你可以先extend类,然后override方法。

http://sandbox.onlinephpfunctions.com/code/61c33b0ce98631986134bf78efcd0391f9b9ab67

<?php
interface MyInterface {
function showUsers($array);
function showUser($i);
}
class MyCLass implements MyInterface {
function showUsers($array = ["Alfred", "Bob", "Claire"])
{
foreach($array as $toto) {
$this->showUser($toto);
}
}
function showUser($i)
{
echo $i;
}
}
// Extend the class in order to override the methods.
class MyCLassDecorator extends MyCLass {
// Also, try removing this method to see what it does. 
function showUsers($array = [1,2,3])
{
foreach($array as $toto) {
$this->showUser($toto);
}
}
function showUser($i)
{
echo "c'est la fete chez $i";
}
}
$myClass = new MyCLassDecorator();
$myClass->showUsers();

编辑

不确定我是否清楚,但问题是,在不使用继承的情况下,您期望的继承行为MyCLass应该如何了解MyCLassDecorator::showUser

foreach($array as $toto) {
// The issue is this line. You're mixing `decorator` and `inheritance`. 
// You should re-think your design. This will not work.
$this->showUser($toto);
}

最新更新