MVC and subClass

From: Leslie Saffrey (lsaffrey_at_myrealbox.com)
Date: 06/06/04


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);
  }
}



Relevant Pages

  • Re: XmlSerializer Collection with Collections
    ... What you can do here is hide the OptionList from serialization as follows: ... > public class TestSerializer ... > public int AddQuestion(Question question) ... > public Question(string QuestionText, string Type, int Score, bool ...
    (microsoft.public.dotnet.xml)
  • XmlSerializer Collection with Collections
    ... public class TestSerializer ... public int AddQuestion(Question question) ... public Question(string QuestionText, string Type, int Score, bool ...
    (microsoft.public.dotnet.xml)
  • Re: invoke
    ... String theClass) ... public class Ehg extends Applet ...
    (comp.lang.java.help)
  • Re: What is the compiler complaining about?
    ... public class Main { ... public static void main{ ... int w = 50; ... BUILD FAILED (total time: 0 seconds) ...
    (comp.lang.java.programmer)
  • Re: What is the compiler complaining about?
    ... public class Main { ... public static void main{ ... int w = 50; ... images/giffer.gif", 1, 1, w, h, pixels, 0, w); ...
    (comp.lang.java.programmer)

Loading