MVC and subClass
From: Leslie Saffrey (lsaffrey_at_myrealbox.com)
Date: 06/06/04
- Next message: Robert C. Martin: "Re: Why is Hungarian Notation Considered Evil?"
- Previous message: Phlip: "Why are Comments Considered Evil?"
- Next in thread: Michaël Willemot: "Re: MVC and subClass"
- Reply: Michaël Willemot: "Re: MVC and subClass"
- Reply: Daniel T.: "Re: MVC and subClass"
- Reply: Robert C. Martin: "Re: MVC and subClass"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 6 Jun 2004 16:00:58 +0100
Hi,
I'm trying to write a java program that uses the Model-View-Controller pattern and I've come across something I don't like.
The View is simply text input/output and no is gui is involved at this stage. I believe the model should not be aware of the view and therefore should not access System.out directly.
My problem is this:
Class D1 and D2 etc are subclass of class B, D1 and D2 has extra attributes and methods to class B.
Class C being the controller has a list of D1 and D2 objects
The View has to print the list of objects, but I also want the extra info in the subclasses.
I can only do this by using instanceof and if/else statements which I believe is bad when more subclasses are added to the system. I don't really want to and a show method in class B because of MVC principles.
Is there some way of overcoming this?
Simplistic code included.
Any ideas , TIA
public class B
{
protected int i;
protected String s;
B(int ii, String ss)
{
i = ii;
s = ss;
}
public int getI()
{
return i;
}
public String getS()
{
return s;
}
}
public class D1 extends B
{
D1(int ii, String ss)
{
super(ii, ss);
}
public int s()
{
return i * i;
}
}
public class D2 extends B
{
private int j;
D2(int ii, String ss, int jj)
{
super(ii, ss);
j = jj;
}
public int getJ()
{
return j;
}
}
import java.util.ArrayList;
public class V
{
public void show(ArrayList l)
{
int i;
B b;
for(i = 0; i < l.size(); i++){
b = (B)l.get(i);
System.out.print( b.getI() + " " + b.getS() );
if ( b instanceof D2 ){
System.out.print(" " + ((D2)b).getJ());
}
// else if ( b instanceof Dx )
// {
// System.out.print( ...
// }
System.out.println("");
}
}
}
import java.util.ArrayList;
public class C
{
public static void main(String[] args)
{
ArrayList list = new ArrayList();
V v = new V();
list.add(new D1(1, "one"));
list.add(new D1(3, "three"));
list.add(new D2(4, "four", 5));
list.add(new D1(5, "five"));
v.show(list);
}
}
- Next message: Robert C. Martin: "Re: Why is Hungarian Notation Considered Evil?"
- Previous message: Phlip: "Why are Comments Considered Evil?"
- Next in thread: Michaël Willemot: "Re: MVC and subClass"
- Reply: Michaël Willemot: "Re: MVC and subClass"
- Reply: Daniel T.: "Re: MVC and subClass"
- Reply: Robert C. Martin: "Re: MVC and subClass"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|