9 Replies Latest reply: Feb 16, 2011 2:22 PM by jschellSomeoneStoleMyAlias RSS

    should an instance know the file from it's been loaded?

    810805
      Hi everybody.

      I have a class that contains some configuration data; sometimes i want to modify the fields of my instance and save it back to the original file.
      public MyClass { public String documentName, fontName /*and so on*/;}
      The question is: do you provide the class with a field that represent the file it has been loaded from?
      Or should you create a sort of "Factory" class that is responsable for reading/writing the class to file?
      I'm looking for a best-practise!

      Thanks in advance
      agostino
        • 1. Re: should an instance know the file from it's been loaded?
          796440
          Agostino wrote:
          Hi everybody.

          I have a class that contains some configuration data; sometimes i want to modify the fields of my instance and save it back to the original file.
          public MyClass { public String documentName, fontName /*and so on*/;}
          The question is: do you provide the class with a field that represent the file it has been loaded from?
          No.

          And even if you did, it sounds like you're suggesting either A) modifying the .class file to hold the new initial value or B) modifying the .java file and recompiling. Both of those approaches are icky bad.

          Depending on your overall scope, execution context, requirements, etc., you could choose between a properties file, a database, the Preferences API, or some other approach. But definitely not modifying the class file.
          • 2. Re: should an instance know the file from it's been loaded?
            810805
            Thanks, however my question was more simple, i wasn't speaking about modifying the .class, but the class adding a field, like this
            public File file;
            From the oop point of view, this represents a "mesh", and i'm intresed come up with a correct "pattern".
            Thanks
            Agostino
            • 3. Re: should an instance know the file from it's been loaded?
              tschodt
              Agostino wrote:
              Hi everybody.

              I have a class that contains some configuration data; sometimes i want to modify the fields of my instance and save it back to the original file.
              public MyClass { public String documentName, fontName /*and so on*/;}
              The question is: do you provide the class with a field that represent the file it has been loaded from?
              It must be in the bytecode in the class file;
              C:\> javap MyClass
              Compiled from "MyClass.java"
              • 4. Re: should an instance know the file from it's been loaded?
                tschodt
                Agostino wrote:
                Thanks, however my question was more simple, i wasn't speaking about modifying the .class, but the class adding a field, like this
                public File file;
                From the oop point of view, this represents
                inheritance
                public MyClass { public String documentName, fontName /*and so on*/;}
                public MyEnhancedClass extends MyClass { public File file; }
                • 5. Re: should an instance know the file from it's been loaded?
                  tschodt
                  Agostino wrote:
                  ... the class file it has been loaded from?
                  Are you looking for
                      URL url = getClass().getProtectionDomain().getCodeSource().getLocation();
                  • 6. Re: should an instance know the file from it's been loaded?
                    810805
                    Ok, this one is the kind of reasoning i'm looking for.

                    If i go the inheritance way, the MyEnhancedClass will be exactly what I would obtain putting all the fields in my original class.

                    The advantage could be creating MyEnhancedClass instances, that contain all the fields.

                    But then distinguish two use cases:
                    - the library-code uses MyClass references, because they don't need to know the enanced extra files
                    - the application-code uses MyEnhancedClass because they use the extra field to manage load/save operations for that instance

                    Effectively this can be a good solution.
                    thanks
                    Agostino
                    • 7. Re: should an instance know the file from it's been loaded?
                      jschellSomeoneStoleMyAlias
                      Agostino wrote:
                      Ok, this one is the kind of reasoning i'm looking for.

                      If i go the inheritance way, the MyEnhancedClass will be exactly what I would obtain putting all the fields in my original class.

                      The advantage could be creating MyEnhancedClass instances, that contain all the fields.

                      But then distinguish two use cases:
                      - the library-code uses MyClass references, because they don't need to know the enanced extra files
                      - the application-code uses MyEnhancedClass because they use the extra field to manage load/save operations for that instance

                      Effectively this can be a good solution.
                      thanks
                      Agostino
                      No.

                      If you want/need a generic configuration solution then you create/find one that does exactly that.

                      Then your other classes use those other classes.

                      You don't use inheritence.
                      • 8. Re: should an instance know the file from it's been loaded?
                        810805
                        No.

                        If you want/need a generic configuration solution then you create/find one that does exactly that.

                        Then your other classes use those other classes.
                        Ok, so I will have a configuration class (MyClass) and a Reader/Writer, my code that use exactly that type.

                        Then i need to read/write from and to disk my configuration. So i will have a "reader and writer" class that will use MyClass producing instances when reading and creating/updating files when writing.


                        This approach "declares" the purpose of the reader/writer class, while inheritance does not.
                        I can see inheritance "changes" the purpose of the class: MyClass was a configuration class. MyEnhancedClass is not a 'more-specialized' configuration class, it adds (writing/reading) features not "strictly" configuration related.

                        Is this the reason why you say:
                        >
                        You don't use inheritence.
                        ?

                        thanks in advance
                        Agostino
                        • 9. Re: should an instance know the file from it's been loaded?
                          jschellSomeoneStoleMyAlias
                          Agostino wrote:
                          Is this the reason why you say:
                          You don't use inheritence.
                          ?
                          Yes.