lundi 26 novembre 2018

What design pattern to use in this program?

This is a program that displays two tables inside a JFrame component. The first table uses a awt.List object and the other table is a TableModel instance inside a JScrollPane object. Below code shows how those objects are added to the Pane.

ProductDisplay.java

public class ProductDisplay extends JFrame
{
    private static final long serialVersionUID = 1L;

    public ProductDisplay()
    {
        super("Die Telematik-Produkte");
        setLF();             //set look and feel
        setCloseClick();     //set close on window close click
        InputFile f = new InputFile("products.txt");
        Vector<String> prod = new Vector<String>();
        //read in product list
        String s = f.readLine();
        List alist = new List();

        while(s != null)
        {
            prod.addElement(s);
            s = f.readLine();
        }
        JPanel p = new JPanel();
        getContentPane().add(p);
        p.setLayout(new GridLayout(1,2));

        JPanel pleft = new JPanel();
        JPanel pright = new JPanel();
        p.add(pleft);
        p.add(pright);
        pleft.setLayout(new BorderLayout());
        pright.setLayout(new BorderLayout());

        //add in customer view as list box
        pleft.add("North", new JLabel("Customer view"));

        // Here, ProductList and ProductTable are added to the Pane object. 
        // Now, a new class Lister.java has to be created and represent both 
        // objects depending on the second constructor parameter
        pleft.add("Center", new ProductList(prod));           // This row ...
        //pleft.add("Center", new Lister(prod, Lister.LIST)); // ...should be replaced by this one

        //add in execute view as table
        pright.add("North", new JLabel("Executive view"));

        pright.add("Center", new ProductTable(prod));           // And this row ...
        //pright.add("Center", new Lister(prod, Lister.TABLE)); // ...should be replaced by this one


        setSize(new Dimension(400,300));
        setVisible(true);
    }
    //-----------------------------------------  
    private void setCloseClick()
    {
        //create window listener to respond to window close click
        addWindowListener(new WindowAdapter() 
        {
            public void windowClosing(WindowEvent e) {System.exit(0);}
        });
    }
    //------------------------------------------
    private void setLF()
    {
        // Force SwingApp to come up in the System L&F
        String laf = UIManager.getSystemLookAndFeelClassName();
        try {
            UIManager.setLookAndFeel(laf);
        }
        catch (UnsupportedLookAndFeelException exc) 
        {System.err.println("Warning: UnsupportedLookAndFeel: " + laf);}
        catch (Exception exc) {System.err.println("Error loading " + laf + ": " + exc);
        }
    }

    //---------------------------------------------

    static public void main(String argv[])
    {
        new ProductDisplay();
    }
}

The to be created class Lister.java accepts as parameters a products Vector and a String stating what type of table to represent.

ProductList.java

public class ProductList extends java.awt.List
{
    private static final long serialVersionUID = 1L;

    public ProductList(Vector<String> products)
    {
        super(products.size());    //for compatibility
        for (int i = 0; i < products.size(); i++)
        {
            //take each strig apart and keep only
            //the product names, discarding the quntities
            String s = products.elementAt(i);
            int index = s.indexOf("--");  //separate qty from name
            if(index > 0)
                add(s.substring(0, index));
            else
                add(s);
        }
    }
}

ProductTable.java

public class ProductTable extends JScrollPane
{
    private static final long serialVersionUID = 1L;

    JTable table;

    public ProductTable(Vector<String> list)
    {
        table = new JTable(new prodModel(list));
        getViewport().add(table);
    }
}
class prodModel implements TableModel
{

    int rows;

    int columns;

    Vector<String> prodNames;

    Vector<String> quantities;

    public prodModel(Vector<String> products)
    {
        rows  = products.size();
        columns = 2;
        prodNames = new Vector<String>();
        quantities =  new Vector<String>();
        for(int i=0; i< products.size(); i++)
        {
            String s = products.elementAt(i);
            int index = s.indexOf("--");  //separate qty from name
            if(index > 0)
            {
                prodNames.addElement(s.substring(0, index));
                quantities.addElement(s.substring(index+2).trim());
            }
            else
                prodNames.addElement(s);

        }
    }
}

So basically, the to be created class Lister.java has to be able to "mutate" into both objects. Multiple inheritance is not possible in Java so extending both classes is not possible. My limited understanding on design patterns is that this could be solved using a Factory Method or maybe a Facade, but im not even sure. My Lister.java looks as follows:

Lister.java

public class Lister{

    public static final String LIST = "LIST";
    public static final String TABLE = "TABLE";

    public Lister(Vector<String> product, String type){
        if(type.equals(LIST)) {

        }
        if(type.equals(TABLE)) {

        }
    }   
}

Aucun commentaire:

Enregistrer un commentaire