Adobe Dreamweaver Forums



Last 10 THreads :         problems linking PDFs (Last Post : Alan - Replies : 1 - Views : 2 )           »          Changing RadioButton Label (Last Post : justintoflex - Replies : 2 - Views : 5 )           »          Table Borders MIssing in JavaHelp (Last Post : soxmann - Replies : 3 - Views : 8 )           »          Clock .getUTC not working (Last Post : bolszo - Replies : 2 - Views : 3 )           »          cfimage error (Last Post : masoud_amen - Replies : 2 - Views : 3 )           »          Dragging components (Last Post : hsfrey - Replies : 2 - Views : 4 )           »          Is file too large? (Last Post : tweaked_eye - Replies : 0 - Views : 1 )           »          Could not find the included template (Last Post : azuro28 - Replies : 2 - Views : 3 )           »          flash loader reappears after movie plays (Last Post : lyshamo - Replies : 3 - Views : 4 )           »          How to turn IMAGE into MOVIECLIP??? (Last Post : Snufferson - Replies : 7 - Views : 8 )           »         


Home Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
User Info Statistics
Go Back   Adobe Dreamweaver Forums > Other Macromedia/Adobe Products > Flex
 
Tags:



Reply
  #1 (permalink)  
Old 11-21-2008, 03:43 PM
webvalue
 
Posts: n/a
Diggs:
Default A question about using get and set methods

Hi,

I am looking at the code of Adobe's example code PhotoViewer
(http://www.adobe.com/devnet/flex/sam...hoto_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!



Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-21-2008, 05:03 PM
Amy Blankenship
 
Posts: n/a
Diggs:
Default Re: A question about using get and set methods


"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/sam...hoto_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


Reply With Quote
  #3 (permalink)  
Old 11-21-2008, 05:53 PM
myIP
 
Posts: n/a
Diggs:
Default Re: A question about using get and set methods

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.

Reply With Quote


  #4 (permalink)  
Old 11-21-2008, 07:03 PM
webvalue
 
Posts: n/a
Diggs:
Default Re: A question about using get and set methods

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?

Reply With Quote
  #5 (permalink)  
Old 11-21-2008, 07:42 PM
Amy Blankenship
 
Posts: n/a
Diggs:
Default Re: A question about using get and set methods


"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.


Reply With Quote
  #6 (permalink)  
Old 11-21-2008, 07:42 PM
Amy Blankenship
 
Posts: n/a
Diggs:
Default Re: A question about using get and set methods


"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.


Reply With Quote


Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



© Camley Interactive (camley.info) 2008 - all logos and images are copywrite their respective owners.
Proud member of the Camley Interactive Network
All times are GMT. The time now is 11:52 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0 ©2007, Crawlability, Inc.
Cheap Car Insurance - Compare Motor Insurance
Endsleigh Car Insurance Natwest Car Insurance
More Than Car Insurance Norwich Union Car Insurance
Prudential Car Insurance Zurich Car Insurance
Inactive Reminders By Mished.co.uk