2 Replies Latest reply: Dec 29, 2010 8:45 AM by jschellSomeoneStoleMyAlias RSS

    Need solution for Design  Pattern Question

    706710
      Hi,
      I have a Design pattern Question in Java/J2EE

      Problem Statement::
      Suppose there are 2 files as below
      XYZ.properties file --> name = value pair
      abc.xml file --> attribute=value pair.

      Both the above files have pairs.

      The question is "Design a component/jsp page such that you can read,edit,save the name,value pairs of either a Property file or xml file.?"
      So which Design pattern would you suggest i should be using in Java.?
      The name,values pair are then fetched and displayed in another jsp page where i should be able to read,edit,save the name,value pairs such that
      1>read operation results in reading the name,value pair
      2>Edit operation results in editing the name,value pair
      3>Save operation results in saving the value for the corresponding name,value pair in database and consequently updates either the property/xml file.

      My Initial Analysis : i would use a Factory design pattern since i have 2 files which have name,value pairs and at run time decide which one to choose depending on the name of file invoked,Once done i would pass the parameters to jsp file for Read,edit and save operation.Save would save the name,value pair to Database but i dont know how will it update the corresponding value for that name in either the property/xml file as well.

      Is my Understanding correct ? If not please provide a code/design/solution in java for the same,such that READ,EDIT,SAVE operation for pairs in either ".properties file" or ".xml file" are carried out?
        • 1. Re: Need solution for Design  Pattern Question
          jduprez
          I have a Design pattern Question in Java/J2EE
          Why do you think it's a pattern question? Does the exercise specifically recommends using a pattern? If not, you can design a solution with basic OO principles and "if" statements, it's not obvious you need a pattern at all.
          Problem Statement::
          Suppose there are 2 files as below
          XYZ.properties file --> name = value pair
          abc.xml file --> attribute=value pair.
          OK. Are these name/value pairs supposed to be the same information in two different formats, or do they represent different information? Must there be some consistency between them?
          Both the above files have pairs.
          ;)
          The question is "Design a component/jsp page such that you can read,edit,save the name,value pairs of either a Property file or xml file.?"
          A JSP application to update a pair of files seems a strange architectural choice.
          Do I understand well that those are files stored on the same server as the JSP engine is running?
          Is the system supposed to support multiple concurrent users? If so you have a bigger problem (now that might deserve a "pattern").
          So which Design pattern would you suggest i should be using in Java.?
          The name,values pair are then fetched and displayed in another jsp page where i should be able to read,edit,save the name,value pairs such that
          1>read operation results in reading the name,value pair
          2>Edit operation results in editing the name,value pair
          3>Save operation results in saving the value for the corresponding name,value pair in database and consequently updates either the property/xml file.
          I don't get a few things:
          - when you "edit" (but not "save"), where is the information stored?
          - if you have a database, why bother updating the property/XML file?

          If your problem is to update the content of the DB, it's strange you start your post by mentioning the files instead.
          My Initial Analysis : i would use a Factory design pattern since i have 2 files which have name,value pairs and at run time decide which one to choose* depending on the name of file invoked
          What is the type of the "one" you chose?
          A factory method , or an abstract factory (just to remind you that factory is not a pattern in itself, and that you should describe more accurately your design) is usually meant to return an instance of one of the subclasses of a common class declared to be returned by the factory method, or to hide how the instance returned is created. But you do have to design which type of instance suits your requirements.
          Returning a java.io.File object, as your posting suggests, misses the point that the content of the file will certainly need to be differently formatted, so it would be an awkward choice.
          That is, if you want to use a factory (which isn't absurd). But you can also use other patterns, or no pattern at all.
          Once done i would pass the parameters to jsp file for Read,edit and save operation
          Which parameters? Are you talking about the query parameters in the URL, or about an object that represents the key/value pairs contained in the file?
          Once done i would pass the parameters to jsp file for Read,edit and save operation
          Warning: except for an exercise, you shouldn't have business logic such as "save" in a JSP. Instead such business logic should be implemented in a model class (EJB, or POJO), or if it's a simple exercise, in a servlet class. JSPs should only be used to build HTML display out of Java objects created/updated by the business logic components.

          Note that even as an exercise, those practice are questionable, since they teach you evil habits of which you won't get loose until you have understood the evilness in them...
          Save would save the name,value pair to Database but i dont know how will it update the corresponding value for that name in either the property/xml file as well.*
          Well, assuming you are not afraid by the concurrent multi-user update of a server-side file, you can simply regenerate the whole file each time by iterating over the appropriate key/value pairs in the DB.

          Feel free to give more info about your requirements, there much too much room for interpretation remaining; forum users will gladly help if they have enough information, but they won't want to spend time designing around unclear assumptions.

          Best regards,

          J.
          • 2. Re: Need solution for Design  Pattern Question
            jschellSomeoneStoleMyAlias
            Certainly looks like a homework problem to me.

            The statement of the problem doesn't lead to a pattern. It leads to an implementation.

            As a pattern the Bridge is appropriate but overkill. As stated in GoF one uses that because one wants to avoid inheritence whereas the statement of this problem would be carried out by doing just that.

            It has nothing to do with Factory. Factory is used to solve problems in creating objects. The problem description doesn't even address creation.