C
craigslist.jg
Hi,
Let's say I have an instance of ClassA, which has an instance variable
holding a list of numbers as an array (for simplicity's sake). Let's
call this instance variable, arrayA. Let's assume all instVars are
public.
Now, ClassB needs to have access to arrayA, and delete some members
within the array.
Based on OOP principles, what's the better / cleaner implementation
for this?
1. Create a couple of instVars in ClassB:
instVar1 : reference to ArrayA
instVar2: new array keeping track of deleted members
Within ClassA, instantiate ClassB, and set instVar1.
When ClassB is done, within ClassA, delete members from arrayA based
on instVar2.
ClassA doStuff()
classB = new ClassB
classB.instVar1 = arrayA
classB.compileRemovedItems()
arrayA.removeAll(classB.instVar2)
ClassB compileRemovedItems()
instVar2 = setRemovedItems()
2. As in (1), create a couple of instVars, but instantiate the
instVar1 within classB
ClassA doStuff()
classB = new ClassB
classB.compileRemovedItems()
arrayA.removeAll(classB.instVar2)
ClassB compileRemovedItems()
instVar1 = classA.arrayA
instVar2 = setRemovedItems()
3. From ClassB, access arrayA directly, and remove the items there.
ClassA doStuff()
classB = new ClassB
classB.compileRemovedItems()
ClassB compileRemovedItems()
instVar1 = classA.arrayA
instVar2 = setRemovedItems()
instVar1.removeAll(instVar2)
With 1, classB has no dependencies on classA, but I have to keep on
setting instVar1 at every point I need to call compileRemovedItems.
With 2, classB sets instVar1 from classA so I save that extra step.
3 is even more dependent on classA, as it is accessing and modifying
arrayA directly.
Thanks for reading.
Let's say I have an instance of ClassA, which has an instance variable
holding a list of numbers as an array (for simplicity's sake). Let's
call this instance variable, arrayA. Let's assume all instVars are
public.
Now, ClassB needs to have access to arrayA, and delete some members
within the array.
Based on OOP principles, what's the better / cleaner implementation
for this?
1. Create a couple of instVars in ClassB:
instVar1 : reference to ArrayA
instVar2: new array keeping track of deleted members
Within ClassA, instantiate ClassB, and set instVar1.
When ClassB is done, within ClassA, delete members from arrayA based
on instVar2.
ClassA doStuff()
classB = new ClassB
classB.instVar1 = arrayA
classB.compileRemovedItems()
arrayA.removeAll(classB.instVar2)
ClassB compileRemovedItems()
instVar2 = setRemovedItems()
2. As in (1), create a couple of instVars, but instantiate the
instVar1 within classB
ClassA doStuff()
classB = new ClassB
classB.compileRemovedItems()
arrayA.removeAll(classB.instVar2)
ClassB compileRemovedItems()
instVar1 = classA.arrayA
instVar2 = setRemovedItems()
3. From ClassB, access arrayA directly, and remove the items there.
ClassA doStuff()
classB = new ClassB
classB.compileRemovedItems()
ClassB compileRemovedItems()
instVar1 = classA.arrayA
instVar2 = setRemovedItems()
instVar1.removeAll(instVar2)
With 1, classB has no dependencies on classA, but I have to keep on
setting instVar1 at every point I need to call compileRemovedItems.
With 2, classB sets instVar1 from classA so I save that extra step.
3 is even more dependent on classA, as it is accessing and modifying
arrayA directly.
Thanks for reading.