5 Replies Latest reply on Nov 21, 2008 11:40 AM by Newsgroup_User

    A question about using get and set methods

    webvalue Level 1
      Hi,

      I am looking at the code of Adobe's example code PhotoViewer ( http://www.adobe.com/devnet/flex/samples/photo_explorer/)

      In file CarouselView.mxml, I don't understand how the get gallery() and set gallery() methods are used.

      How and where is the method get gallery() is invoked? And why did the author have to use these methods that are so confusing?

      Thanks!
        • 1. Re: A question about using get and set methods
          Level 7

          "webvalue" <webforumsuser@macromedia.com> wrote in message
          news:gg6kr6$1ib$1@forums.macromedia.com...
          > Hi,
          >
          > I am looking at the code of Adobe's example code PhotoViewer
          > ( http://www.adobe.com/devnet/flex/samples/photo_explorer/)
          >
          > In file CarouselView.mxml, I don't understand how the get gallery() and
          > set
          > gallery() methods are used.
          >
          > How and where is the method get gallery() is invoked? And why did the
          > author
          > have to use these methods that are so confusing?

          These are what's known as getters and setters.

          Where it says gallery="{gallery}" in each of the mxml component
          instantiations below, that calls the setters on each component, unless Matt
          used a public var, which is what happened in the ThumbnailView.

          <ThumbnailView id="thumbnailView" gallery="{gallery}"
          slideshowView="views.selectedChild = slideshowView"
          carouselView="views.selectedChild = carouselView"/>

          <CarouselView id="carouselView" gallery="{gallery}"
          slideshowView="views.selectedChild = slideshowView"
          thumbnailView="views.selectedChild = thumbnailView"/>

          <SlideShowView id="slideshowView" gallery="{gallery}"
          thumbnailView="views.selectedChild = thumbnailView"
          carouselView="views.selectedChild = carouselView"/>

          If you look at the Carousel component, you can see that it has several
          places that refer to gallery, even though no gallery variable exists per se,
          such as this:

          private function showPrev():void
          {
          if (gallery.selected > 0)
          {
          nextPhoto.setStyle("icon", ICON_RIGHT);
          gallery.selected = gallery.selected - 1;
          carousel.rotateRight();

          if (gallery.selected <= 0)
          prevPhoto.setStyle("icon", ICON_LEFT_DISABLED);
          }
          }

          Those places are calling the getter function.

          Developers use getters and setters instead of public variables for a variety
          of reasons. One reason you might choose to do this is that you expect to do
          some additional processing in the getter or the setter before setting or
          returning the value. Another reason is because you expect the component to
          be extended later and you want to allow for the possibility of the extending
          developer to customize the functionality. Yet another reason is that you're
          implanting and interface that calls for getters and setters instead of
          public variables. A fourth reason is that you want to make a property
          read-only or write-only, so you only provide a getter or a setter.

          As to why Matt did it that particular way...In the Flex 2 days Flex was
          purely the purview of either complete codeheads coming from the Java world
          or people who were determined to become so. So it probably never occurred
          to him that using a basic feature of the language would confuse anyone
          looking at it.

          Hope this clarifies;

          Amy


          • 2. Re: A question about using get and set methods
            injpix Level 3
            A getter and setter are also known as an accessor and mutator respectively. A mutator (setter) mutates the state of the object it resides in. An accessor (getter) simply returns the property that mutated it (as Amy mentioned, a getter may execute some other code when returning the property, however I have never seen that in a conventional way).

            For instance, this class as an example, I would use “_gallery” instead of just “gallery” when referring internally to the property. Therefore developers should know that it’s a property of its object and not just a variable.

            Also, its worth to mention here, Java doesn’t have keywords such get and set. To order to make a getter and setter, you would need to prefix get or set to the name of the method. Such as:

            public String getColor()
            {
            return this.color;
            }
            public void setColor(String color)
            {
            this.color = color;
            }

            …which is lame. The actually name of a property or method should be just a name. It shouldn’t have any logic associated with it, at least not in that way.
            • 3. Re: A question about using get and set methods
              webvalue Level 1
              Thank you Amy and myIP for your explanations. Very appreciative for your looking into the code. I do understand why developers should use getter and setter methods (from Moock's book), but not in that file.

              It's unclear to me how the the property expression of the main page gallery="{gallery}" invokes the getter methods. Yes, it says the component it is linking has a gallery variable and its value should be the same as that of the main page. But CarouselView doesn't have definition for gallery, except the getter and setter, and _gallery.

              And how _gallery change's its value?
              • 4. Re: A question about using get and set methods
                Level 7

                "webvalue" <webforumsuser@macromedia.com> wrote in message
                news:gg6vcj$en7$1@forums.macromedia.com...
                > Thank you Amy and myIP for your explanations. Very appreciative for your
                > looking into the code. I do understand why developers should use getter
                > and
                > setter methods (from Moock's book), but not in that file.
                >
                > It's unclear to me how the the property expression of the main page
                > gallery="{gallery}" invokes the getter methods.

                It invokes the setter in the child component, and the getter in the parent
                component.

                Yes, it says the component it
                > is linking has a gallery variable and its value should be the same as that
                > of
                > the main page. But CarouselView doesn't have definition for gallery,
                > except the
                > getter and setter, and _gallery.

                Yes, that's what I said. It retrieves _gallery when gallery is referred to
                and puts it into the public gallery property of the child component, which
                means it actually calls the setter function, which sets *its* _gallery
                variable.

                > And how _gallery change's its value?

                The setter does that.


                • 5. Re: A question about using get and set methods
                  Level 7

                  "myIP" <webforumsuser@macromedia.com> wrote in message
                  news:gg6s4p$asp$1@forums.macromedia.com...
                  >A getter and setter are also known as an accessor and mutator respectively.
                  >A
                  > mutator (setter) mutates the state of the object it resides in. An
                  > accessor
                  > (getter) simply returns the property that mutated it (as Amy mentioned, a
                  > getter may execute some other code when returning the property, however I
                  > have
                  > never seen that in a conventional way).

                  public function get fullName():String {
                  return _firstName + ' ' + _lastName;
                  }

                  Consider yourself educated.

                  > For instance, this class as an example, I would use ?_gallery? instead of
                  > just
                  > ?gallery? when referring internally to the property. Therefore developers
                  > should know that it?s a property of its object and not just a variable.

                  Yes, internally I'd do the same thing. But developers developing example
                  files to demonstrate language features of a new language don't always do
                  something that, 3 years down the line, seems like obvious best practice.

                  > Also, its worth to mention here, Java doesn?t have keywords such get and
                  > set.
                  > To order to make a getter and setter, you would need to prefix get or set
                  > to
                  > the name of the method. Such as:
                  >
                  > public String getColor()
                  > {
                  > return this.color;
                  > }
                  > public void setColor(String color)
                  > {
                  > this.color = color;
                  > }
                  >
                  > ?which is lame. The actually name of a property or method should be just
                  > a
                  > name. It shouldn?t have any logic associated with it, at least not in
                  > that way.

                  And Visual Basic has getters and setters like in ActionScript, and has for
                  years. Obviously, many developers have a different idea of what's useful
                  and powerful than you do. Look at how set data() is conventionally
                  overridden in itemRenderers to make use of the invalidation system.