OOP: mutliple references to same Object: how?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Pieter

    OOP: mutliple references to same Object: how?

    Hi,

    In my object oriented application (VB.NET 2.0, Windows Forms), a lot of
    objects are opened in different forms by a user. For instance (a stupid
    exemple, but it shows the easiest what's happening):
    -two instances of the Company-Object: MyCompany1 and MyCompany2, both of
    them are poiting to the same Company: for isntance the Company Microsoft.

    The problem is: if the user makes changes to one of the 2 instances, these
    changes are not automaticly made to the other instance. So what I somehow
    would need is a way that there is some local cache of the opened Business
    Objects, and in one is asked that is already opened, that a reference is
    given to the same object.

    This must definetly be something that is a common practice in OOP, but I
    can't find out how to do this?

    And a secondary question: As we will start using Hibernate in some time: is
    this kind of technique usable with NHibernate?

    Thanks a lot in advance,

    Pieter


  • Peter Duniho

    #2
    Re: OOP: mutliple references to same Object: how?

    On Fri, 02 Mar 2007 18:04:01 +0800, Pieter
    <pieterNOSPAMco ucke@hotmail.co mwrote:
    [...] So what I somehow
    would need is a way that there is some local cache of the opened Business
    Objects, and in one is asked that is already opened, that a reference is
    given to the same object.
    >
    This must definetly be something that is a common practice in OOP, but I
    can't find out how to do this?
    Well, to start with as long as you, instead of creating a new object,
    simply copy an existing reference, then that's all you need to do to
    create the situation you're asking about.

    Now, how to make sure that happens depends on the situation. But
    generally speaking, when you try to instantiate a new instance of an
    object, the first thing you would do is check to see if you already have
    one that meets the criteria you have.

    In your example, presumably you would use "Microsoft" as the key to
    instantiating an object. In that case, you maintain a list of all the
    objects you've already instantiated and search the list before you
    instantiate a new object. So when creating a new object of key
    "Microsoft" , you first look in your list of existing objects to see if one
    labeled "Microsoft" is already there. If it is, rather than creating a
    new object, you just return the reference to the existing one.

    The exact implementation will vary according to your needs. But that's
    the basic idea. You'll note that you need to implement the caching
    mechanism yourself. There is no uniform "local cache" of objects to
    handle this for you.
    And a secondary question: As we will start using Hibernate in some time:
    is this kind of technique usable with NHibernate?
    Huh? What does any of the above have to do with NHibernate?

    Pete

    Comment

    • Peter Duniho

      #3
      Re: OOP: mutliple references to same Object: how?

      On Fri, 02 Mar 2007 18:14:56 +0800, Peter Duniho
      <NpOeStPeAdM@nn owslpianmk.comw rote:
      >And a secondary question: As we will start using Hibernate in some
      >time: is this kind of technique usable with NHibernate?
      >
      Huh? What does any of the above have to do with NHibernate?
      Sorry...I posted while trying to change that text. :)

      Anyway, what I should have said is simply that since you have to implement
      the object cache yourself, it is persisted just as any of your data would
      be persisted under NHibernate.

      Comment

      • Pieter

        #4
        Re: OOP: mutliple references to same Object: how?

        "Peter Duniho" <NpOeStPeAdM@nn owslpianmk.comw rote in message
        news:op.toj3260 t8jd0ej@petes-computer.local. ..
        On Fri, 02 Mar 2007 18:04:01 +0800, Pieter
        <pieterNOSPAMco ucke@hotmail.co mwrote:
        >
        The exact implementation will vary according to your needs. But that's
        the basic idea. You'll note that you need to implement the caching
        mechanism yourself. There is no uniform "local cache" of objects to
        handle this for you.
        Thanks, and aren't such a caching tools available yet? It's impossible that
        I would be the first to want such a thing?
        Huh? What does any of the above have to do with NHibernate?
        Because of the fact that NHibernate is an ORM, if I would have written
        NHibernate, it would have such a functionality implemented... :-)


        Comment

        • Joanna Carter [TeamB]

          #5
          Re: mutliple references to same Object: how?

          "Pieter" <pieterNOSPAMco ucke@hotmail.co ma écrit dans le message de news:
          O9L1cILXHHA.418 8@TK2MSFTNGP06. phx.gbl...

          | In my object oriented application (VB.NET 2.0, Windows Forms), a lot of
          | objects are opened in different forms by a user. For instance (a stupid
          | exemple, but it shows the easiest what's happening):
          | -two instances of the Company-Object: MyCompany1 and MyCompany2, both of
          | them are poiting to the same Company: for isntance the Company Microsoft.
          |
          | The problem is: if the user makes changes to one of the 2 instances, these
          | changes are not automaticly made to the other instance. So what I somehow
          | would need is a way that there is some local cache of the opened Business
          | Objects, and in one is asked that is already opened, that a reference is
          | given to the same object.
          |
          | This must definetly be something that is a common practice in OOP, but I
          | can't find out how to do this?

          My guess is that you are creating two instances of the same Company into two
          separate references :

          Company myCompany1 = new Company(...);

          Company myCompany2 = new Company(...);

          What you really want is to have two references to the same instance. This is
          possibly easiest achieved by using a Class Factory rather than the
          constructors of the classes, and is something that a lot of persistence
          mechanisms use.

          public static class CompanyFactory
          {
          private static Dictionary<int, WeakReferencein stances = new
          Dictionary<int, WeakReference>( );

          public static Company CreateCompany(i nt id)
          {
          if (instances.Cont ainsKey(id)
          return (Company) instances[id].Target; // this will resurrect the
          target if necessary

          Company newCompany = new Company(id);
          // populate new Company
          WeakReference newInstance = new WeakReference(n ewCompany, true);
          instances.Add(i d, newInstance);
          return newCompany;
          }
          }

          Then this static method can be called from more than one place and will
          always return the same instance. You also need to set up a strategy for
          periodically sweeping this list and removing all WeakReferences whose
          targets have been GCed.

          Joanna

          --
          Joanna Carter [TeamB]
          Consultant Software Engineer


          Comment

          • Pieter

            #6
            Re: mutliple references to same Object: how?

            "Joanna Carter [TeamB]" <joanna@not.for .spamwrote in message
            news:u2igpiLXHH A.4132@TK2MSFTN GP06.phx.gbl...
            Then this static method can be called from more than one place and will
            always return the same instance. You also need to set up a strategy for
            periodically sweeping this list and removing all WeakReferences whose
            targets have been GCed.
            Thanks a lot!
            And how can I now which are GCed?


            Comment

            • Ornette

              #7
              Re: OOP: mutliple references to same Object: how?

              Hello,

              Look at the Singleton Pattern :

              See also design patterns on Microsoft' :
              Find official documentation, practical know-how, and expert guidance for builders working and troubleshooting in Microsoft products.


              Ornette.


              "Pieter" <pieterNOSPAMco ucke@hotmail.co ma écrit dans le message de
              news:OMMHGRLXHH A.480@TK2MSFTNG P02.phx.gbl...
              "Peter Duniho" <NpOeStPeAdM@nn owslpianmk.comw rote in message
              news:op.toj3260 t8jd0ej@petes-computer.local. ..
              >On Fri, 02 Mar 2007 18:04:01 +0800, Pieter
              ><pieterNOSPAMc oucke@hotmail.c omwrote:
              Thanks, and aren't such a caching tools available yet? It's impossible
              that I would be the first to want such a thing?

              Comment

              • Joanna Carter [TeamB]

                #8
                Re: mutliple references to same Object: how?

                "Pieter" <pieterNOSPAMco ucke@hotmail.co ma écrit dans le message de news:
                e1h3s2MXHHA.510 8@TK2MSFTNGP06. phx.gbl...

                | And how can I now which are GCed?

                Check the target for null or the WeakReference's IsAlive property, depending
                if you have a long or short reference. See the help for WeakReference for
                more ideas :-)

                Joanna

                --
                Joanna Carter [TeamB]
                Consultant Software Engineer


                Comment

                • Cor Ligthert [MVP]

                  #9
                  Re: mutliple references to same Object: how?

                  Pieter,

                  They should change in both, however be aware that sometimes people including
                  me do this..


                  Private Pieter as new Belg

                  Private sub EU
                  dim Pieter as new Walon
                  End Sub

                  Now that although Pieter is created as Belg globaly, when it is used inside
                  the Eu he will the threaten as walon.

                  Cor


                  "Pieter" <pieterNO

                  SPAMcoucke@hotm ail.comschreef in bericht
                  news:O9L1cILXHH A.4188@TK2MSFTN GP06.phx.gbl...
                  Hi,
                  >
                  In my object oriented application (VB.NET 2.0, Windows Forms), a lot of
                  objects are opened in different forms by a user. For instance (a stupid
                  exemple, but it shows the easiest what's happening):
                  -two instances of the Company-Object: MyCompany1 and MyCompany2, both of
                  them are poiting to the same Company: for isntance the Company Microsoft.
                  >
                  The problem is: if the user makes changes to one of the 2 instances, these
                  changes are not automaticly made to the other instance. So what I somehow
                  would need is a way that there is some local cache of the opened Business
                  Objects, and in one is asked that is already opened, that a reference is
                  given to the same object.
                  >
                  This must definetly be something that is a common practice in OOP, but I
                  can't find out how to do this?
                  >
                  And a secondary question: As we will start using Hibernate in some time:
                  is this kind of technique usable with NHibernate?
                  >
                  Thanks a lot in advance,
                  >
                  Pieter
                  >

                  Comment

                  • PS

                    #10
                    Re: OOP: mutliple references to same Object: how?


                    "Pieter" <pieterNOSPAMco ucke@hotmail.co mwrote in message
                    news:OMMHGRLXHH A.480@TK2MSFTNG P02.phx.gbl...
                    "Peter Duniho" <NpOeStPeAdM@nn owslpianmk.comw rote in message
                    news:op.toj3260 t8jd0ej@petes-computer.local. ..
                    >On Fri, 02 Mar 2007 18:04:01 +0800, Pieter
                    ><pieterNOSPAMc oucke@hotmail.c omwrote:
                    >>
                    >The exact implementation will vary according to your needs. But that's
                    >the basic idea. You'll note that you need to implement the caching
                    >mechanism yourself. There is no uniform "local cache" of objects to
                    >handle this for you.
                    >
                    Thanks, and aren't such a caching tools available yet? It's impossible
                    that I would be the first to want such a thing?
                    It is generally known as a Registry. You ask it for an object based on a
                    key. If it already has it it returns the (same) instance to you. If it does
                    not then it deelgates the construction to a factory / builder and returns
                    this object.

                    PS
                    >
                    >Huh? What does any of the above have to do with NHibernate?
                    >
                    Because of the fact that NHibernate is an ORM, if I would have written
                    NHibernate, it would have such a functionality implemented... :-)
                    >

                    Comment

                    • Peter Duniho

                      #11
                      Re: OOP: mutliple references to same Object: how?

                      On Fri, 02 Mar 2007 18:19:31 +0800, Pieter
                      <pieterNOSPAMco ucke@hotmail.co mwrote:
                      Thanks, and aren't such a caching tools available yet? It's impossible
                      that I would be the first to want such a thing?
                      Surely implementations of this do exist in various libraries. However,
                      that may or may not be required. In the simplest case, coding it yourself
                      is relatively trivial.

                      There are, of course, a wide variety of added optional features one might
                      include in such a caching system. If you desire more behavior than just
                      that which you've asked about, it might be worthwhile to explore existing
                      libraries. But if all you need is to maintain a list of objects and
                      inspect the list before creating a new instance of an object, returning a
                      previously-created object if some specific data matches, I'm not sure I
                      see the need for an external library.

                      Pete

                      Comment

                      • Pieter

                        #12
                        Re: mutliple references to same Object: how?

                        "Cor Ligthert [MVP]" <notmyfirstname @planet.nlwrote in message
                        news:eMFEqxPXHH A.1396@TK2MSFTN GP05.phx.gbl...
                        Now that although Pieter is created as Belg globaly, when it is used
                        inside the Eu he will the threaten as walon.
                        Which would be actually horrible and very offensive, because I'm actually
                        Flemish, not Wallon ;-)


                        Comment

                        • oscar.acostamontesde@googlemail.com

                          #13
                          Re: mutliple references to same Object: how?

                          On Mar 6, 2:24 pm, "Pieter" <pieterNOSPAMco u...@hotmail.co mwrote:
                          "Cor Ligthert [MVP]" <notmyfirstn... @planet.nlwrote in messagenews:eMF EqxPXHHA.1396@T K2MSFTNGP05.phx .gbl...
                          >
                          Now that although Pieter is created as Belg globaly, when it is used
                          inside the Eu he will the threaten as walon.
                          >
                          Which would be actually horrible and very offensive, because I'm actually
                          Flemish, not Wallon ;-)
                          Make Company class singleton or static, then all references will point
                          to the same instances.

                          Comment

                          Working...