php - How can I ensure that an object only belongs to one collection, while maintaining the DRY principle? -
i'm trying solve problem in php code, i've been unable solve in other code well.
i have object must belong 1 , 1 collection. , accessing these objects perspective of collection. other code wouldn't expected find object without going through collection.
so, how i've handled in past give object reference collection. in way, attempt satisfy object can belong 1 collection. add object collection.
public function __construct(collection $c) { $this->setcollection($c); } public setcollection(collection $c) { $this->collection = $c; $c->addobject($this); }
this obvious contradiction "don't repeat yourself". , comes because requirements of, needing easy access collection of objects , requiring objects belong in 1 collection, @ odds each other. object knows it's in collection , collection knows object in collection. if either gets out of sync system broken.
and problems become obvious when start trying make code move object 1 collection another. or when deleting object.
so, has found solution kind of problem? there should try? in end can make work lot of double checking, seems there has better way.
i'm not sure follow line of reasoning end game of 'double checking'... what's wrong code this?
class collection { public function addmember(member $m) { $m->setcollection($this); // add underlying data structure } public function removemember(member $m) { // remove underlying data structure } } class member { private $collection = null; public function setcollection(collection $c) { if($this->collection) $this->collection->removemember($this); $this->collection = $c; } public __destruct() { if($this->collection) $this->collection->removemember($this); } }
syntax off, treat psuedo-code, not tested, use references needed, other disclaimers etc.
this great design pattern friend classes/methods useful proper encapsulation, prevent other collection object setting or removing member collection. want think long , hard methods want used entry points , right thing in cases without falling recursion loop. framework/library class this, unit tests make life easier.
yeah, can see may worried having if
check before doing adding, removing or destroying. that's nature of smart & safe pointer/reference pattern. dry principle isn't avoiding redundancy of safety checks avoiding having multiple copies of data or redundant implementations of algorithms. yes, each of methods in pseudo code technically algorithm, here's few things chew on (okay, they're same-ish thing, stated different perspectives):
- they run in constant time (provided avoid recursion pitfalls mentioned).
- seriously: shortest algorithm ever.
- if you're worried optimizing out half of
if
statements in library, you're missing forest (a single) tree. optimization time better spent on making code easier use, easier maintain, , reducing complexity of algorithms. doesn't less complex constant time algorithm. - you're not going peg processor doing
if
statement on every add or remove. peg processor runningo(n^2)
algorithm on largen
.
to sum up, no there isn't better way implement safe, doubly-linked data structure. have check safety on each link , that's the way it's going be.
Comments
Post a Comment