Hi,
I have 3 classes A, B and C; I want to achieve following behavior (I am
calling it “pure object oriented designing”) with them.
-
An instance of Class C should not exist without instance of
Class B
and instance of Class B should not exist without instance of Class A.
-
Class A should be the only class accessible externally, i.e.
one
should not be able to create/modify/delete instances of class B and
class C
outside the scope of class A.
-
For modifying member variables of class B and class C, an
external
client should use class A.
-
class A has multiple instances of class B associated with it
and
similarly class B has multiple instances of class C associated with it.
Following are the classes with required methods and variables;
class A
#array of objects of class B associated with this instance
@b_objs
#method to create instance of B and add it to above array
#methods to modify member variables of an instance of B and C (class C
is
defined below)
end
class B
@b1
@b2
#array of objects of class C
@c_objs
#method to create instance of C and add it to above array
#methods to modify member variables of an instance of C
end
class C
@c1
@c2
end
How would I achieve 4 points listed above?
One solution I thought was to have nested classes as below,
Class A
Class B
Class C
end
end
end
above approach satisfies all the points, but for point 3 above(For
modifying
member variables of class B and class C, an external client should use
class
A), there would be an overhead of adding a large number of wrapper
methods
for members of classes B and C.
Is there any cleaner solution in ruby by which I can achieve above
requirements?