dongyou8087 2012-01-18 16:06
浏览 61
已采纳

在没有多重继承的情况下,在这个PHP类结构中使用什么是合适的设计模式?

I have an abstract class called Node. It contains a constructor that takes a row from my database, and instantiates basic information. All pieces of content on my website extend this class - Person, Event, Project, etc.

3 of these extending classes are special - when they are constructed, in addition to pulling values from the database, they also need to query a web-service; if the web-service provides values that are different from the ones given in the DB, they need to save to the DB.

In a multiple-inheritance capable language, this would be fairly simple; any one of these classes would extend both Node, and APIData, or something like that. Without MI, I'm not sure how to handle this. Using an interface would not be helpful, as that provides no concrete implementations.

The decorator pattern is sometimes recommended as a substitue for some features of MI, but I don't have enough experience to determine if this is the appropriate choice. Any suggestions?

  • 写回答

3条回答 默认 最新

  • dongxuan1660 2012-01-18 16:25
    关注

    Since the APIData class will take functionallity from your Node class, you should simply extend it. Here is some pseudo code:

    abstract class APIData extends Node {
    
        public function __construct($data) {
            parent::__construct($data);
            $this->checkData();
        }
    
        protected function checkData() {
            // load data from webservice
            $data = $this->loadData();
    
            // check if data is the same
            foreach($data as $item => $value) {
                if ($this->data[$item] != $value) {
                    // save in database
                }
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器