jeudi 4 février 2016

Design Patterns - Using generic object to update a GUI

My problem is design related. I will first explain what i'm working on and then my current approach which I think should improve but I can't figure out how, hence the question!

So in a nutshell, I parse a raw string to obtain name,value pairs which is then displayed on a GUI. Now this raw string can belong to either type01 or type02. The type determines the view I display the name,value pairs on.

type01 - Contains messy xml, and since I want to preserve the xml hierarchy, my view comprises of a JXTreeTable.

type02 - The name,value data is relatively easy to extract and has a flat structure. Hence my view for this is a JTable

I use a cardlayout for the two views.

Now let me explain my implementation. Unfortunately i'm not in a position to upload any code, so please bear with me. Anyway this is more of a design issue :)

  1. String received. (can be either type01 or type02)
  2. Factory class creates appropriate Parser object. (type01Parser or type02Parser both super classed by Parser)
  3. Parser does the parsing and extracts the name,value details to Object.
  4. Parser getter method is used to retrieve the Object. which I should be able to use to update my GUI (?)

My problem lies in the Object that contains the extracted details. type01Parser finally creates a tree structure. Where each node represents a name,value pair. The Object is basically the root node. type02Parser creates an ArrayList, where each element is a name,value pair. The Object is the ArrayList in this case.

I can hack my way from here, but I really want to follow good design patterns. I believe the return objects should also be super-classed in some way, encapsulating the update functionalities? Also the fact that i'm updating two completely different UI elements makes things a bit more confusing. Some general guidance would be highly appreciated.

Although I can't post any code, let me try to explain everything using dummy classes :) Note that i've left out a lot but the general idea is captured. Hopefully

The factory class basically scans the string and determines the type and creates the required parser object

...
FactoryParser factory = new FactoryParser(rcvdString);
Parser parser = factory.getBestParser();
Object generalObjectHere = parser.getReturnObject();
...

//whaat next??

Abstract Parser class

public abstract class Parser{

    Object returnObject=null;
    String str;

    public Parser(String s){
        str=s;
    }

    public void setReturnObject(Object obj){
        returnObject=obj;
    }  

    public Object getReturnObject(){
        return returnObject;
    }  

}

Type01Parser

public class Type01Parser{

    public Type01Parser(){

        initParser();
        setReturnObject(processAndDoStuff());
    }

    //bunch of methods to process the xml which is in String format.  

}

Type02Parser

public class Type02Parser{

    public Type02Parser(){

        initParser();
        setReturnObject(processAndDoStuff());
    }

    //bunch of methods to process the string

}

Aucun commentaire:

Enregistrer un commentaire