views:

210

answers:

4

Let's say I have an abstract parent class called "shape", and that there are multiple subclasses (triangle, square, circle... ). I want to define an abstract method in the parent "shape" class which all subclasses must implement, let's call it "draw". So all shape subclasses must provide the "draw()" method. But, the draw method takes a parameter of type "Stencil", and, not every shape subclass can use just any stencil...

So there is one abstract "shape" class, multiple shape subclasses, and multiple stencils. I need a draw method defined in the shape class. A square might use Stencil1 and the circle might use Stencil2.

I'm guessing that generics would do the trick, but I'm not sure. Each shape subclass needs to define the draw method with a specific stencil because these classes are used by other classes as well, and the compiler should force all programmers to call the draw methods with the stencil that is supported by that class. We can't define an abstract method like "public abstract void draw(Stencil s)" because then the programmer could pass in any stencil to the square class, whereas the square class only supports "Stencil1"

Any ideas?

Update1: Should add that the shape class doesn't care which stencil is used by the subclass, but since the subclasses are used in other classes too, it's important that the draw method is defined so that only the supported stencil is accepted by the compiler.

A: 

If you want this to be caught at compile time, the following options come to mind:

  • Create a set of abstract stencils. Particularly if you think you can group them. So if you'll have multiple "square" stencils, create a SquareStencil abstract type and derive the concrete instances from that. You can even create them as a subset of the abstract Stencils class.
  • Overload the draw method. There's no need for it to be generic. Let the compiler help you by choosing the right method for the job.
altCognito
If I were to overload, that would make the problem worse, wouldn't it? I don't want multiple versions of draw; I want one version that only accepts the correct stencil for that class.
+1  A: 

I think you should probably reconsider your initial design.

Of course, you could get around this by using instanceof, etc. However, this will result in a very confusing API (if that's what you are using it for).

Zack
I'm sure the design sounds confusing from my description, but it's not really that bad
+7  A: 
public abstract class Shape<S extends Stencil>
{
   public abstract void draw( S stencil );
}

public class Square extends Shape<Stencil1>
{
   public void draw( Stencil1 stencil )
   {
     stencil.letsdo();
     stencil.some();
     stencil.drawing();
   }
}

public class Circle extends Shape<Stencil2>
{
   public void draw( Stencil2 stencil )
   {
      stencil.some();
      stencil.more();
      stencil.drawing();
   }
}
alasdairg
wouldn't the implementation method signatures of draw disagree with the method signature of the abstract method draw?
e5
No, actually. I just tested this in Eclipse to be sure, and it works fine.
Chamelaeon
No. draw() is defined to take an S which has to be of type Stencil or a subclass. In Square/Circle, you just specify the Stencil subclass you want to use - as its still a Stencil, it doesn't conflict with the superclass's method signature.
alasdairg
Thank you, this is exactly what I am after.
There seem to be some bad side-effects with this - for example you can't draw the contents of a List<Shape<? extends Stencil>>.
DJClayworth
This solution seems wrong, what if you go back and want to draw with a different stencil. Is it a new instance every time you want to draw with a new Stencil? How does this prevent the wrong Stencil from being used with the Shape? Color me confused.
altCognito
So, instead of overloading draw, we're creating clones of objects. Ooook. I guess that's great.
altCognito
All this does is to enforce at _compile time_ that when you call draw() on a Square, you must supply a Stencil1 (or subclass thereof). When you call draw() on a Circle, you must supply a Stencil2 (or subclass thereof). The compiler will flag an error and refuse to compile your code if it does not comply.
alasdairg
@DJClayworth - But if you don't know what specific type of Shape each item is, how can you know which type of Stencil you must supply as a parameter to its draw() method? The compiler can't tell either - hence why this won't work.
alasdairg
If you want to be able to draw with different Stencils, then the best approach is probably to have those stencils extend a BaseBlahStencil and have the Shape be of <BaseBlahStencil>
matt b
A: 

Define an abstact Stencil and let the subclass constructor decide which stencil class to use.

private Stencil s;

public void draw(){
   privateDraw(s)
}

private abstract void privateDraw(Stencil s);
e5