所以我想我会为这些项目创建一个Voteable接口:
interface Voteable { public function vote( User $user,$value ); }
class VotingRepository { public function castVote( Voteable $item,User $user,$value ) { // save the these values,along with the value $itemId = $item->getId(); $userId = $user->getId(); } }
目前,存储库将是一个数据库.该数据库将为每种类型的投票提供链接表:
> eventVote
> commentVote
> userVote
因此,这实质上意味着每个域对象都需要另一个表来投票.这对工厂来说是个好人吗?这种情况下的VotingRepositoryFactory?换句话说,像:
class VotingRepositoryFactory { createVotingRepository( $type ) { switch( $type ) { case 'event': // create a voting repository with EventVote table return new VotingRepository( new EventVoteTable() ); case 'comment': // create a voting repository with CommentVote table return new VotingRepository( new CommentVoteTable() ); case 'user': // create a voting repository with UserVote table return new VotingRepository( new UserVoteTable() ); } } }
然后,从域对象(例如在这种情况下评论)中将它们全部捆绑在一起,我看起来像这样:
class Comment implements Voteable { public function construct() { $this->_repository = VotingRepositoryFactory::createVotingRepository( 'comment' ); } public function vote( User $user,$value ) { $this->_repository->castVote( $this,$user,$value ); } }
这有意义吗?