You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@flex.apache.org by mark goldin <ma...@gmail.com> on 2014/11/13 20:33:20 UTC
Singleton question
Here is my singleton:
[Bindable]
public class ModelLocator extends EventDispatcher implements IModelLocator
{
private static var _instance:ModelLocator = new ModelLocator();
public var test:String;
public function ModelLocator()
{
if (_instance != null){
throw new Error("You can have only one ModelLocator");
}
}
public static function getInstance():ModelLocator
{
return _instance;
}
}
}
When I am debugging I see that _instance is always different object when I
get an instance of my singleton. Is that right?
Thanks
RE: Singleton question
Posted by Kessler CTR Mark J <ma...@usmc.mil>.
+1
private static var _instance:ModelLocator;
public static function getInstance():ModelLocator
{
if(! _instance)
_instance = new ModelLocator();
return _instance;
}
Only creating new instance when one isn't present. Since it's static, it should only be done once.
-Mark
-----Original Message-----
From: Harbs [mailto:harbs.lists@gmail.com]
Sent: Friday, November 14, 2014 3:54 AM
To: users@flex.apache.org
Subject: Re: Singleton question
No. That’s wrong. It should be:
private static var _instance:ModelLocator;
public static function getInstance():ModelLocator
{
if(! _instance)
_instance = new ModelLocator();
return _instance;
}
(No else, and you need to assign _instance.)
Re: Singleton question
Posted by Harbs <ha...@gmail.com>.
No. That’s wrong. It should be:
private static var _instance:ModelLocator;
public static function getInstance():ModelLocator
{
if(! _instance)
_instance = new ModelLocator();
return _instance;
}
(No else, and you need to assign _instance.)
On Nov 14, 2014, at 7:23 AM, pkumar.flex <pr...@gmail.com> wrote:
> sorry for incomplete reply. below is exact code.
>
> private static var _instance:ModelLocator;
> public static function getInstance():ModelLocator
> {
> if(! _instance)
> return new ModelLocator();
> else
> return _instance;
>
> }
>
> On Fri, Nov 14, 2014 at 10:57 AM, Prashant Kumar <pr...@gmail.com>
> wrote:
>
>> do not create model locator object after class declaration. Use below
>> getInstance() to create model locator object.
>> like this:
>>
>> private static var _instance:ModelLocator;
>> public static function getInstance():ModelLocator
>> {
>> if(! _instance)
>>
>> }
>>
>> On Fri, Nov 14, 2014 at 12:58 AM, mark goldin [via Apache Flex Users] <
>> ml-node+s2333346n8738h63@n4.nabble.com> wrote:
>>
>>> Here is my singleton:
>>> [Bindable]
>>> public class ModelLocator extends EventDispatcher implements
>>> IModelLocator
>>> {
>>> private static var _instance:ModelLocator = new ModelLocator();
>>> public var test:String;
>>>
>>> public function ModelLocator()
>>> {
>>> if (_instance != null){
>>> throw new Error("You can have only one ModelLocator");
>>> }
>>> }
>>> public static function getInstance():ModelLocator
>>> {
>>> return _instance;
>>> }
>>> }
>>> }
>>>
>>> When I am debugging I see that _instance is always different object when
>>> I
>>> get an instance of my singleton. Is that right?
>>>
>>> Thanks
>>>
>>>
>>> ------------------------------
>>> If you reply to this email, your message will be added to the
>>> discussion below:
>>>
>>> http://apache-flex-users.2333346.n4.nabble.com/Singleton-question-tp8738.html
>>> To unsubscribe from Apache Flex Users, click here
>>> <http://apache-flex-users.2333346.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=1&code=cHJhc2hha3VtYXJAZ21haWwuY29tfDF8LTU0MTcyMzE2NA==>
>>> .
>>> NAML
>>> <http://apache-flex-users.2333346.n4.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>>>
>>
>>
>>
>> --
>> *Regards,*
>> Prashant Kumar* | *Mob.: +91 8408811225
>>
>
>
>
> --
> *Regards,*
> Prashant Kumar* | *Mob.: +91 8408811225
>
>
>
>
> --
> View this message in context: http://apache-flex-users.2333346.n4.nabble.com/Singleton-question-tp8738p8770.html
> Sent from the Apache Flex Users mailing list archive at Nabble.com.
Re: Singleton question
Posted by "pkumar.flex" <pr...@gmail.com>.
sorry for incomplete reply. below is exact code.
private static var _instance:ModelLocator;
public static function getInstance():ModelLocator
{
if(! _instance)
return new ModelLocator();
else
return _instance;
}
On Fri, Nov 14, 2014 at 10:57 AM, Prashant Kumar <pr...@gmail.com>
wrote:
> do not create model locator object after class declaration. Use below
> getInstance() to create model locator object.
> like this:
>
> private static var _instance:ModelLocator;
> public static function getInstance():ModelLocator
> {
> if(! _instance)
>
> }
>
> On Fri, Nov 14, 2014 at 12:58 AM, mark goldin [via Apache Flex Users] <
> ml-node+s2333346n8738h63@n4.nabble.com> wrote:
>
>> Here is my singleton:
>> [Bindable]
>> public class ModelLocator extends EventDispatcher implements
>> IModelLocator
>> {
>> private static var _instance:ModelLocator = new ModelLocator();
>> public var test:String;
>>
>> public function ModelLocator()
>> {
>> if (_instance != null){
>> throw new Error("You can have only one ModelLocator");
>> }
>> }
>> public static function getInstance():ModelLocator
>> {
>> return _instance;
>> }
>> }
>> }
>>
>> When I am debugging I see that _instance is always different object when
>> I
>> get an instance of my singleton. Is that right?
>>
>> Thanks
>>
>>
>> ------------------------------
>> If you reply to this email, your message will be added to the
>> discussion below:
>>
>> http://apache-flex-users.2333346.n4.nabble.com/Singleton-question-tp8738.html
>> To unsubscribe from Apache Flex Users, click here
>> <http://apache-flex-users.2333346.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=1&code=cHJhc2hha3VtYXJAZ21haWwuY29tfDF8LTU0MTcyMzE2NA==>
>> .
>> NAML
>> <http://apache-flex-users.2333346.n4.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>>
>
>
>
> --
> *Regards,*
> Prashant Kumar* | *Mob.: +91 8408811225
>
--
*Regards,*
Prashant Kumar* | *Mob.: +91 8408811225
--
View this message in context: http://apache-flex-users.2333346.n4.nabble.com/Singleton-question-tp8738p8770.html
Sent from the Apache Flex Users mailing list archive at Nabble.com.
Re: Singleton question
Posted by "pkumar.flex" <pr...@gmail.com>.
do not create model locator object after class declaration. Use below
getInstance() to create model locator object.
like this:
private static var _instance:ModelLocator;
public static function getInstance():ModelLocator
{
if(! _instance)
}
On Fri, Nov 14, 2014 at 12:58 AM, mark goldin [via Apache Flex Users] <
ml-node+s2333346n8738h63@n4.nabble.com> wrote:
> Here is my singleton:
> [Bindable]
> public class ModelLocator extends EventDispatcher implements IModelLocator
> {
> private static var _instance:ModelLocator = new ModelLocator();
> public var test:String;
>
> public function ModelLocator()
> {
> if (_instance != null){
> throw new Error("You can have only one ModelLocator");
> }
> }
> public static function getInstance():ModelLocator
> {
> return _instance;
> }
> }
> }
>
> When I am debugging I see that _instance is always different object when I
> get an instance of my singleton. Is that right?
>
> Thanks
>
>
> ------------------------------
> If you reply to this email, your message will be added to the discussion
> below:
>
> http://apache-flex-users.2333346.n4.nabble.com/Singleton-question-tp8738.html
> To unsubscribe from Apache Flex Users, click here
> <http://apache-flex-users.2333346.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=1&code=cHJhc2hha3VtYXJAZ21haWwuY29tfDF8LTU0MTcyMzE2NA==>
> .
> NAML
> <http://apache-flex-users.2333346.n4.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>
--
*Regards,*
Prashant Kumar* | *Mob.: +91 8408811225
--
View this message in context: http://apache-flex-users.2333346.n4.nabble.com/Singleton-question-tp8738p8769.html
Sent from the Apache Flex Users mailing list archive at Nabble.com.
Re: Singleton question
Posted by Alex Harui <ah...@adobe.com>.
Mark,
English is an ambiguous language. That’s why it isn’t used as a
programming language. For example, I have no idea what “get an instance”
means. Use the profiler and see what it says. Maybe try posting links to
screenshots of the debugger and code. Don’t attach screenshots to these
emails.
-Alex
On 11/13/14, 1:12 PM, "mark goldin" <ma...@gmail.com> wrote:
>Yes, exactly. I am getting different ID every time I get an instance.
>
>On Thu, Nov 13, 2014 at 3:10 PM, Alex Harui <ah...@adobe.com> wrote:
>
>> Are you referring to where FlashBuilder shows something like:
>>(@1171655c1)
>>
>> It would be odd for that number to change for a Singleton within the
>>same
>> debug session, but the number can certainly change from run to run. You
>> can try the profiler and see how many instances of the Singleton it
>>thinks
>> is getting created.
>>
>> -Alex
>>
>> On 11/13/14, 12:51 PM, "mark goldin" <ma...@gmail.com> wrote:
>>
>> >I meant a value that is shown in Debug Window in Value column when I
>> >select
>> >_instance in Name column . Value has unique number at the end.
>> >
>> >On Thu, Nov 13, 2014 at 2:46 PM, Alex Harui <ah...@adobe.com> wrote:
>> >
>> >> Not sure what “internal name” means but I didn’t see any name
>>property
>> >>on
>> >> the code you supplied. Show us the code for “internal name”.
>> >>
>> >> On 11/13/14, 12:30 PM, "mark goldin" <ma...@gmail.com> wrote:
>> >>
>> >> >The way I know that the object is different is I am checking its
>> >>internal
>> >> >name in the Debugger every time I get an instance.
>> >> >
>> >> >On Thu, Nov 13, 2014 at 2:07 PM, Alex Harui <ah...@adobe.com>
>>wrote:
>> >> >
>> >> >>
>> >> >>
>> >> >> On 11/13/14, 11:33 AM, "mark goldin" <ma...@gmail.com>
>>wrote:
>> >> >>
>> >> >>
>> >> >> >When I am debugging I see that _instance is always different
>>object
>> >> >>when I
>> >> >> >get an instance of my singleton. Is that right?
>> >> >>
>> >> >> How do you know it is a different object?
>> >> >>
>> >> >> It could be that the code generator for [Bindable] is messing
>>things
>> >>up.
>> >> >> You might try switching to [Bindable(“someEvent”)] and manually
>>write
>> >> >>the
>> >> >> getters and setters and have the setters dispatchEvent(new
>> >> >> Event(“someEvent”);
>> >> >>
>> >> >> -Alex
>> >> >>
>> >> >>
>> >>
>> >>
>>
>>
Re: Singleton question
Posted by mark goldin <ma...@gmail.com>.
Yes, exactly. I am getting different ID every time I get an instance.
On Thu, Nov 13, 2014 at 3:10 PM, Alex Harui <ah...@adobe.com> wrote:
> Are you referring to where FlashBuilder shows something like: (@1171655c1)
>
> It would be odd for that number to change for a Singleton within the same
> debug session, but the number can certainly change from run to run. You
> can try the profiler and see how many instances of the Singleton it thinks
> is getting created.
>
> -Alex
>
> On 11/13/14, 12:51 PM, "mark goldin" <ma...@gmail.com> wrote:
>
> >I meant a value that is shown in Debug Window in Value column when I
> >select
> >_instance in Name column . Value has unique number at the end.
> >
> >On Thu, Nov 13, 2014 at 2:46 PM, Alex Harui <ah...@adobe.com> wrote:
> >
> >> Not sure what “internal name” means but I didn’t see any name property
> >>on
> >> the code you supplied. Show us the code for “internal name”.
> >>
> >> On 11/13/14, 12:30 PM, "mark goldin" <ma...@gmail.com> wrote:
> >>
> >> >The way I know that the object is different is I am checking its
> >>internal
> >> >name in the Debugger every time I get an instance.
> >> >
> >> >On Thu, Nov 13, 2014 at 2:07 PM, Alex Harui <ah...@adobe.com> wrote:
> >> >
> >> >>
> >> >>
> >> >> On 11/13/14, 11:33 AM, "mark goldin" <ma...@gmail.com> wrote:
> >> >>
> >> >>
> >> >> >When I am debugging I see that _instance is always different object
> >> >>when I
> >> >> >get an instance of my singleton. Is that right?
> >> >>
> >> >> How do you know it is a different object?
> >> >>
> >> >> It could be that the code generator for [Bindable] is messing things
> >>up.
> >> >> You might try switching to [Bindable(“someEvent”)] and manually write
> >> >>the
> >> >> getters and setters and have the setters dispatchEvent(new
> >> >> Event(“someEvent”);
> >> >>
> >> >> -Alex
> >> >>
> >> >>
> >>
> >>
>
>
Re: Singleton question
Posted by Alex Harui <ah...@adobe.com>.
Are you referring to where FlashBuilder shows something like: (@1171655c1)
It would be odd for that number to change for a Singleton within the same
debug session, but the number can certainly change from run to run. You
can try the profiler and see how many instances of the Singleton it thinks
is getting created.
-Alex
On 11/13/14, 12:51 PM, "mark goldin" <ma...@gmail.com> wrote:
>I meant a value that is shown in Debug Window in Value column when I
>select
>_instance in Name column . Value has unique number at the end.
>
>On Thu, Nov 13, 2014 at 2:46 PM, Alex Harui <ah...@adobe.com> wrote:
>
>> Not sure what “internal name” means but I didn’t see any name property
>>on
>> the code you supplied. Show us the code for “internal name”.
>>
>> On 11/13/14, 12:30 PM, "mark goldin" <ma...@gmail.com> wrote:
>>
>> >The way I know that the object is different is I am checking its
>>internal
>> >name in the Debugger every time I get an instance.
>> >
>> >On Thu, Nov 13, 2014 at 2:07 PM, Alex Harui <ah...@adobe.com> wrote:
>> >
>> >>
>> >>
>> >> On 11/13/14, 11:33 AM, "mark goldin" <ma...@gmail.com> wrote:
>> >>
>> >>
>> >> >When I am debugging I see that _instance is always different object
>> >>when I
>> >> >get an instance of my singleton. Is that right?
>> >>
>> >> How do you know it is a different object?
>> >>
>> >> It could be that the code generator for [Bindable] is messing things
>>up.
>> >> You might try switching to [Bindable(“someEvent”)] and manually write
>> >>the
>> >> getters and setters and have the setters dispatchEvent(new
>> >> Event(“someEvent”);
>> >>
>> >> -Alex
>> >>
>> >>
>>
>>
Re: Singleton question
Posted by mark goldin <ma...@gmail.com>.
I meant a value that is shown in Debug Window in Value column when I select
_instance in Name column . Value has unique number at the end.
On Thu, Nov 13, 2014 at 2:46 PM, Alex Harui <ah...@adobe.com> wrote:
> Not sure what “internal name” means but I didn’t see any name property on
> the code you supplied. Show us the code for “internal name”.
>
> On 11/13/14, 12:30 PM, "mark goldin" <ma...@gmail.com> wrote:
>
> >The way I know that the object is different is I am checking its internal
> >name in the Debugger every time I get an instance.
> >
> >On Thu, Nov 13, 2014 at 2:07 PM, Alex Harui <ah...@adobe.com> wrote:
> >
> >>
> >>
> >> On 11/13/14, 11:33 AM, "mark goldin" <ma...@gmail.com> wrote:
> >>
> >>
> >> >When I am debugging I see that _instance is always different object
> >>when I
> >> >get an instance of my singleton. Is that right?
> >>
> >> How do you know it is a different object?
> >>
> >> It could be that the code generator for [Bindable] is messing things up.
> >> You might try switching to [Bindable(“someEvent”)] and manually write
> >>the
> >> getters and setters and have the setters dispatchEvent(new
> >> Event(“someEvent”);
> >>
> >> -Alex
> >>
> >>
>
>
Re: Singleton question
Posted by Alex Harui <ah...@adobe.com>.
Not sure what “internal name” means but I didn’t see any name property on
the code you supplied. Show us the code for “internal name”.
On 11/13/14, 12:30 PM, "mark goldin" <ma...@gmail.com> wrote:
>The way I know that the object is different is I am checking its internal
>name in the Debugger every time I get an instance.
>
>On Thu, Nov 13, 2014 at 2:07 PM, Alex Harui <ah...@adobe.com> wrote:
>
>>
>>
>> On 11/13/14, 11:33 AM, "mark goldin" <ma...@gmail.com> wrote:
>>
>>
>> >When I am debugging I see that _instance is always different object
>>when I
>> >get an instance of my singleton. Is that right?
>>
>> How do you know it is a different object?
>>
>> It could be that the code generator for [Bindable] is messing things up.
>> You might try switching to [Bindable(“someEvent”)] and manually write
>>the
>> getters and setters and have the setters dispatchEvent(new
>> Event(“someEvent”);
>>
>> -Alex
>>
>>
Re: Singleton question
Posted by mark goldin <ma...@gmail.com>.
The way I know that the object is different is I am checking its internal
name in the Debugger every time I get an instance.
On Thu, Nov 13, 2014 at 2:07 PM, Alex Harui <ah...@adobe.com> wrote:
>
>
> On 11/13/14, 11:33 AM, "mark goldin" <ma...@gmail.com> wrote:
>
>
> >When I am debugging I see that _instance is always different object when I
> >get an instance of my singleton. Is that right?
>
> How do you know it is a different object?
>
> It could be that the code generator for [Bindable] is messing things up.
> You might try switching to [Bindable(“someEvent”)] and manually write the
> getters and setters and have the setters dispatchEvent(new
> Event(“someEvent”);
>
> -Alex
>
>
Re: Singleton question
Posted by Alex Harui <ah...@adobe.com>.
On 11/13/14, 11:33 AM, "mark goldin" <ma...@gmail.com> wrote:
>When I am debugging I see that _instance is always different object when I
>get an instance of my singleton. Is that right?
How do you know it is a different object?
It could be that the code generator for [Bindable] is messing things up.
You might try switching to [Bindable(“someEvent”)] and manually write the
getters and setters and have the setters dispatchEvent(new
Event(“someEvent”);
-Alex