lundi 26 novembre 2018

What design pattern would be suitable for this program?

Supose I have to create a GUI application in Java. The layout looks like this: enter image description here

Both panels are JPanel objects. The left one contains an object from class ProductList.java that extends awt.List. The right one contains an object from class ProductTable.java that extends JScrollPane and adds a TableModel implementation to its JViewPort object. Both ProductList and ProductTable are added to the panels like this:

pleft.add(new ProductList()); 

and

pright.add(new ProductTable());  

Now this has to change. A Lister.java class has to replace those classes like this:

pleft.add(new Lister(Lister.LIST));

and

pright.add(new Lister(Lister.TABLE));

I recently started seeing design patterns in java , so my understanding and experience using them is fairly limited. I could think of a factory or decorator design pattern, since the Lister class delivers two objects that are somewhat related (they both are awt.Component objects). What confuses me, is how new Lister() could deliver two different objects using only a String constructor parameter. Im stuck here:

public class Lister{

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

    public Lister(String type){
        if(type.equals(LIST)) {

        }
        if(type.equals(TABLE)) {

        }
    }
}

Aucun commentaire:

Enregistrer un commentaire