doufang2228 2011-08-31 13:15
浏览 130

哪种设计模式适合这种情况?

So far, most of the objects I've been dealing with look very much like the database tables they are based on. So I have a fish object that maps the db.fish table and a fish_transfer object that maps the db.fish_transfer table, etc.

Now I'm working on a class, Provenance that manages data that doesn't easily map to a single database table. Here's the situation:

Each fish has a provenance, ie information about where it was before it entered our system. There are 4 kinds of provenance: 'unknown', 'delivered', 'bred onsite', 'reuse'. All provenances have a fish_id and a date associated and all except 'bred onsite' require a count to be set immediately. Other than that, they are fairly different in the data associated with them and the actions that must occur on their creation/deletion or change.

I had thought to store the provenance type in the fish object. So, on instantiation, fish would call ProvenanceFactory::make($this->fish_id, $this->ptype) and obtain a provenance object $prov of the correct subclass. So what happens if the provenance type is edited?

Possibilities:

  • call $this->prov->delete() and then $this->prov=ProvenanceFactory::make($this->fish_id, $newtype); $this->prov->setall($data);
  • call ProvenanceFactory::morph(&$this->prov,$newtype); $this->prov->setall($data);
  • call $this->prov->morph($newtype); $this->prov->setall($data);
  • call $this->prov->morph($newtype, $data);
  • ??

In all cases, editing the provenance type results in updating or deleting a set of database rows in various tables specific to the Provenance subclass and then creating another set of rows in various other tables.

Which of the above possibilities (or other) would be the best way to go? What design pattern(s) (other than Factory) should I be trying to implement?

  • 写回答

2条回答 默认 最新

  • doumen5895 2011-08-31 13:28
    关注

    I don't want to suggest remapping everything you have done, however have you considered using Class Table Inheritance.

    See : http://martinfowler.com/eaaCatalog/classTableInheritance.html

    So in your example your Provenance table and you can use your discrimator coloumn to identify which state the class is in.

    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c