Exemple #1
0
        $this->personList[spl_object_hash($person)] = $person;
    }
    public function delPerson(Person $person)
    {
        unset($this->personList[spl_object_hash($person)]);
    }
    public function accept(Action $action)
    {
        foreach ($this->personList as $person) {
            $person->accept($action);
        }
    }
}
//客户端代码
$ObjectStructure = new ObjectStructure();
$ObjectStructure->addPerson(new Man('男人'));
$ObjectStructure->addPerson(new Woman('女人'));
$success = new Success('成功');
$failing = new Failing('失败');
$amativeness = new amativeness('恋爱');
$ObjectStructure->accept($success);
$ObjectStructure->accept($failing);
$ObjectStructure->accept($amativeness);
/*
输出如下
男人成功时,背后多有一个伟大的女人
女人成功时,背后多一个不成功的男人
男人失败时,闷头喝酒谁也劝不了
女人失败时,眼泪旺旺谁也不用劝
男人恋爱时,遇事不懂也要装懂
女人恋爱时,遇事懂也装作不懂