I want to create a voting system for which several domain objects can be voted on:
- calendar event
- a comment
- user
So, I decided to create a Voteable interface for these elements:
interface Voteable { public function vote( User $user, $value ); }
I thought this vote method would proxy the repository method, for example:
class VotingRepository { public function castVote( Voteable $item, User $user, $value ) {
At the moment, the repository will be a database. This database will contain binding tables for each type of voting:
- eventVote
- commentVote
- userVote
Thus, this essentially means that each table of the domain requires a different table for the transfer of votes. Will this be a good candidate for the factory? A VotingRepositoryFactory in this case? In other words, something like:
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() ); } } }
Then, linking all this together, from inside the domain objects (for example, a comment in this case), I would look something like this:
class Comment implements Voteable { public function construct() { $this->_repository = VotingRepositoryFactory::createVotingRepository( 'comment' ); } public function vote( User $user, $value ) { $this->_repository->castVote( $this, $user, $value ); } }
It makes sense?
php design-patterns factory
Decent dabbler
source share