如何实现不同类型参数的接口方法

时间:2015-10-06 17:25:35

标签: php oop design-patterns interface

TL;博士

我需要<div class="dealer-addy"> 8726 N. Royal Ln<br> Irving, TX 75063<br> email@aol.com </div> <button>Replace email</button>,其中TypeInterface::generateId(<object> $doc)对于实现接口的每个类都不同。 我该怎样/应该做到这一点?

大家好

我正在破坏我的头脑。

我有一个界面(<object>)需要有一个类似TypeInterface的方法,它会从doc规范中返回一个生成的id,到目前为止一直很好。

问题

这几个文档有共同的方法,如TypeInterface::generateId(Document $doc),但所有属性都是针对每个文档的。 所以从这里我做了以下接口

  • DocumentInterface - 保存所有文档必须具有的方法
  • ObjectAInterface - 保存Document type A属性的方法
  • ObjectBInterface - 保存Document type B属性的方法

因此类TypeA(实现::schema())生成ID,其中包含ObjectBInterface对于TypeB类没有的ObjectAInterface的一些属性/方法

我打算这样做:

TypeInterface

因此public function getDocument(TypeInterface $core, DocumentInterface $document) { $this->client->index([ 'index' => $core->getIndex()->name(), 'type' => $core->name(), 'id' => $core->generateId($document), // HERE! 'body' => $core->schema(), ]); } 应该是通用的,以便与所有$core->generateId($document)一起使用,但特定的TypeA应该与DocumentA有某种关系。

问题:

  • 我怎样才能做到这一点?
  • 这些情况下的最佳做法?
  • 也许我只是错误地思考这个问题,并且有更好的方法来做到这一点,或者根本没有

我用示例代码创建了一个简单的要点 https://gist.github.com/MLoureiro/13cb336bc22ce330511f

0 个答案:

没有答案
相关问题