I have a base class ShapeManager
with a list of shapes which I want to
enumerate()
. Then there is a specialization ColoredShapeManager
which wants
to process specialized ColoredShape
s instead of Shape
s:
+----------------+ +-------------------------------------+
| Shape | | ShapeManager |
|----------------| |-------------------------------------|
| + id: int | | # shapes: List<Shape> |
| | | |
| | | + ShapeManager() { |
| | | shapes.add(new Shape()); |
| | | } |
| | | |
| | | + abstract void enumerate() { |
| | | for (Shape s: shapes) { |
| | | // use s |
| | | } |
| | | } |
+----------------+ +-------------------------------------+
^ ^
| |
+ +
+----------------+ +-------------------------------------+
| ColoredShape | | ColoredShapeManager |
|----------------| |-------------------------------------|
| + color: int | | + ColoredShapeManager() { |
| | | shapes.add(new ColoredShape()); |
| | | } |
| | | |
| | | + abstract void enumerate() { |
| | | for (Shape s: shapes) { |
| | | // use (ColoredShaped) s |
| | | // will fail for Shapes |
| | | } |
| | | } |
+----------------+ +-------------------------------------+
I am unsure whether ShapeManager should share shapes: List<Shape>
with its
children This seems flawed since ColoredShapeManager.enumerate()
wants to
process ColoredShape
s. Hence it would cast the elements, yet some elements
(those added by the base class) are of type Shape
and the cast would fail.
That is:
shapes
.enumerate()
in the child manager should have access to ColoredShape
.Should I rather split the list and create private lists in each of the two managers? Then enumerate in the child would only iterate over "its" type of shapes and call the parent's enumerate()
at the start/end.
Parent and Child classes having same data member in Java. The reference variable of the Parent class is capable to hold its object reference as well as its child object reference. In Java, methods are virtual by default (See this for details).
Types of Bases Strong base – It is a compound that has an ability to remove a proton from a very weak acid. Or they completely dissociate into its ions when in water. Examples are potassium hydroxide (KOH), sodium hydroxide (NaOH).
These Base Types come from the SPBaseType enumeration. Obsolete. Use 0 for discussion boards. These List Definitions come from the SPListTemplateType enumeration.
The reference holding the child class object reference will not be able to access the members (functions or variables) of the child class. It is because compiler uses special run-time polymorphism mechanism only for methods. It is possible to access child data members using parent pointer with typecasting.
You can add the shape type as the "type parameter" for your manager classes. So basically, ColoredShapeManager
can extend ShapeManager<ColoredShape>
and this T
will be the type of your internal List
data structure. Also, if your ColoredShapeManager
doesn't do anything specific with the ColoredShapes
, I would argue that it doesn't even need a new class. But then again it depends on the way you are structuring your app/design.
Would the visitor pattern suit you here?
Shape
public void doSomething(ShapeManager) {
...
}
ShapeManager
abstract void enumerate() {
for (Shape shape: shapes) {
shape.doSomething(this);
}
}
Then you wouldnt need to know the type and each shape derivative could have its own implementation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With