Link to home
Start Free TrialLog in
Avatar of DesignPatterns
DesignPatterns

asked on

Design Pattern - which one?

Hello,
We have a system where the frontend(which is not the GUI)but which simulates a subset of GUI and passes on the commands(requests) and log the response data from the backend.

We feel that there is a scope for improvement in next version of our product.As a sidenote this is in Java.

Now the current design is partitioned into 2 classes in which

Class A ==> subset of commands for Global administrator all implemented as separate functions(methods in Java)

Class B ==> subset of commands for Local administrator all implemented as separate functions(methods in Java)

Each time we get a request we do the checks and then spawn either a Class A /Class B object with the relevant parameters.

Now Class A/B are becoming huge and unmanageable.We are also running into Memory problems(Java is horrible in memory management,yet to improve ,how I wish there was a 'delete object' ,sigh...)

The desired solution will have extensibility in that we can add new commands to either of them easily.

Mind you the data is parsed from the simulated GUI/ and formatted for backend in the above 2 classes for all the implemented commands.

A complete redesign is on the cards and some help in brainstorming with design patterns is sought with justifiction for our client.

Also I was thinking since we are spawning those 2 classes can't we have static functions(methods in Java) for all commands ,so that memory footprint is conserved?And we doont have to spawn objects?

please advise

p.s deliberately keeping low points ,since posted in C++ & Java sections
Avatar of Jod
Jod

What is the lifespan of the Object A and Object B classes and how many concurrent users do you have? In other words, do you need more than one concurrent Object A or Object B?

It is quite possible you can use static methods, but I'd need to know a bit more about how the application is used. Static methods would also help with your memory management problems - reuse of objects in Java is highly recommended.

Could you clarify what you mean by a simulated GUI? Do you mean that you have an interface that accepts remote commands and then passes them on? How do the users acccess the application?
Avatar of DesignPatterns

ASKER

Jod,

>>>>>What is the lifespan of the Object A and Object B classes and how many concurrent users do you have?

Many users,so i think that these objects be in memory throughout.

>>>>>In other words, do you need more than one concurrent Object A or Object B?

sorry for not clarifying earlier but
our 'command' is just a static final int (much like const int)with which the frontend sends like

guiRequestHandler.sendCommand(int command,String data);

sendCommand() is synchronized so when we send command ,a new Class A object or Class B object is invoked and then callback the backend.

so I think that no need for taht and those objects r not threads.

>>>>>
Could you clarify what you mean by a simulated GUI?...

it's a command line utility.
Does every sendCommand() create a new object?
Or does it just invoke an existing one if possible and how do you currently store and track your Object A and Object B's?
Jod,
this is the reply I posted at C++ section

KangaRoo,

I am pushing for a RegEx package for parsing but i dont know if they would approve as it's a third party package ,but thanks anyway.FYI it is a readymade implementation of the Interpreter customised for my need.

yonat,
>>>>>
What does sendCommand do?
Is it a part of the program you are responsible for, or are you just calling it?

basically the cmd line is sitting at the server side ,client sends a command with optional data.If client wants a list for example then data is balank but response data is the list of whatver they want.

If client wants to do a save/apply then they send relevant data & response is OK or if some prob ,some indication.

>>>>>What does the part of the system that you are developing responsible for?

so I mostly have to redesign the name-value pairs parsing,validation per name-value pair,1 record validation and lastly inter record validation

>>>>>Getting command codes and deciding where to send them, or deciding what action to perform when a certain command is invoked? Or maybe something else?

so the comand codes are fixed (static int ,KangaRoo I dont understand C++ syntax that much ,sorry.) in some file client just calls them and sends data so I have to just call the guiRequest object's sendCommand()with some diff mapped opcodes and the changed data after parsing,validation& required formatting

gysbert1
yonat & u r absolutely right in that I will have to use multiple patterns.

yonat & KangaRoo mentioned for me to use the Command but I was not thinking right ,yonat went and said abt the dictionary using the Proactor.

what I was thinking of was this,sorry if it's in Java but I cant think C++


//to implement Command Pattern
//this si much like a class having a pure virtual function

public interface Validator
{
      public boolean validate(String oneRecord);
}

//First Concrete implementation of Validator
public class Type000Validator implements Validator
{
      //bunch of private methods & data PLUS the common validate()

      public boolean validate(String oneRecord)
      {
         //validate the recoerd & return if success/failure
      }
}

public class Field
{
      private Field [] toSearch=null;
      private String fieldName=null;
      private boolean isCompulsory=false;
      private boolean isBlankAllowed=false;

      public Field(String f,boolean isC,boolean isB)
      {
       this.fieldName=f;
         this.isCompulsory=isC;
         this.isBlankAllowed=isB;
      }

      public String getFieldName()
      {
         return fieldName;
      }
      public void setFieldName(String name)
      {
         if(name!=null && name.trim().length()>0)
            fieldName=name;
      }

      ///similar methods for the 2 booleans.

      public static void setFieldArray(Field[]f)
      {
          this.toSearch=f;
      }
      public static int isNamePresent(String name)
      {
         if(toSearch==null)
            throw new Exception("Array is not initialized");

         int toReturn=-1;
         for(int i=toSearch.length;--i>=0;)
         {
           if(toSearch[i].getFieldName().equals(name))
           {
              toReturn=i;
              break;
           }
       }

       ///not found
       return toReturn;
    }
}

public interface GenericParser
{
   public String genericParsingMethod(Field[] names,String data,Validator v);
}

public Class A implements GenericParser
{
    private static final Field [] type000Array = new Field[3];

    static
    {
            //this type000 consists of 3 name-value pairs only
            type000Array = new Field[3];
            type000Array[0]=new Field("IDName",true,false);
            type000Array[1]=new Field("Address",true,false);
            type000Array[2]=new Field("StreetNumber",false,true);
      }

    public String saveType000Command(int command,String data)
      {
            String inFormat=genericParsingMethod(type000Array ,data,new Type000Validator());
        guiReqHandelr(command,inFormat);
      }

    //only concrte implementation of GenericParser interface
    public String genericParsingMethod    (Field[]names,String data,Validator v)
    {
    /*
    (1)pick up 1 record using regex package
    (2)validate the record using
     v.validate(oneRecord)
    */
    }
}


the only thing I think is lacking is that I cant seem to make this dynamic i.e if I dont have the class Field & an Array I will have 1 class for each command so If rigth now we have 80 commands 80 class files! Can anybody suggest any changes?

I think that I have implemented Command pattern in the Validator ,is this OK?

the interpreter is the Regex package

I believe that the Visitor can be applied as GenericTypeXXXCommand
but I cant figure out how right now
to answer ur questions
(1)Does every sendCommand() create a new object?

No,existing object for that session

(2)how do you currently store and track your Object A and Object B's?

We allocate it every time we get a new command request from the SAme Client,i.e client sends list() ,we alloc 'A' then sometime later save() again we alloc 'A' ,etc .. so depending on command alloc either 'A' or 'B'.
Makes more sense now - I'm a bit snowed under today, but I'll have a think about this and get back to you a bit later...
I have implemented the command pattern+interpreter from Regex package and sent it to my senior for approval,will tell u the result sometime later

Thanks
Sorry I have had so little time to check back on this this week.

If your boss is like most bosses though, he won't have a clue what you are going on about and sign off whatever you suggest.

But I am just a cynic...

(If I feel I have something useful to add I will get back to you but it needs a bit of thought and I am very short on time at the moment)
TAN TAN TAN TAN TAN TAN TAN TAN TAN TAN TAN TAN TAN TAN TAN TAN TAN TAN TAN TAN TAN TAN TAN TAN TAN TAN TAN TAN TAN TAN TAN TAN TAN TAN TAN TAN TAN TAN TAN TAN TAN TAN TAN TAN TAN TAN TAN TAN TAN TAN TAN TAN TAN TAN TAN TAN TAN TAN TAN TAN

Jod,
many thanks to the guys at the C++ section, my boss has approved the initial design,we are going to sit down and hammer the issues in detail.
Will need your help much later ,as I will ve snowed under till Sunday atleast.

Thanks
Fine discussion
i am afraid to reprt that they have rejected the Reg ex package and i have to do the whole parsing myself.

somebody shoudl invent a Boss Pattern ,i.e when Boss is doing this we do taht etc...

probably we can fill some pretty cool patterns
:)
Jod,
can you lock this question so I can give you points?
Thank you DesignPatterns but I really don't feel I have earned the points here - feel free to delete this question and save the points up for a future question.

It is an interesting problem but I didn't really have time to think about it - in general most of the questions here require less thought than this so it is a shame that I don't have time for the challenge but there you go...

Some links that will help are:

www.ootips.org

though the code is in C++ the info is very useful.


Also have a look into JavaCC as a parser generator - your boss cannot reject this as what it does is generate a program that does parsing for you based on a specified grammar that you provide.

Comes with many pre-prepared grammars and may just save you a lot of time:

http://www.metamata.com/JavaCC/FAQ/
Can you explain the Visitor pattern ,I have been trying to understand it and couldnt,if u agree then I will post a new question for you to answer.
ASKER CERTIFIED SOLUTION
Avatar of Jod
Jod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes it is ,but I will read it in detail later on.anyway making this a PAQ now.