Link to home
Start Free TrialLog in
Avatar of laubrass
laubrass

asked on

Which is the best implement?!?

Hi Experts,

we use Singletons in some classes of our application.

here an example...
-----------------------------------------------------------
class name: Features
-----------------------------------------------------------
private string _Something;
public string Something
{
Get { return(_Something); }
Set { _Something = value; }
}

private static Features instance = null;

public static Features GetInstance()
            {
                  if(instance == null)
                  {
                        instance = new ExportContoller();
                  }

                  return instance;
            }

private Features()
{
}

(...)
-----------------------------------------------------------

and i'm asking myself which call method do i have to use... i mean which is the best one!?!?!

Method 1:
Features keyID = Features.getInstance();
keyID.Something = "AGAH876876";

or

Method 2:
Features.getInstance().Something = "AGAH876876";

Does they have different Lifetime?!?

thks!

laubrass
Avatar of Sinclair
Sinclair

At first glance, the two calls appear identical to me, because Features.instance is static.
Avatar of Dmitry G
I believe both calls are identical and are compiled into same code.

The only iisue I can see from the above code is:

instance = new ExportContoller();

'instance' is of type 'Features'. Why's that?
Avatar of laubrass

ASKER

ExportContoller is a copy and paste error while posting.
It should ha been
instance = new Features();
ASKER CERTIFIED SOLUTION
Avatar of mjmarlow
mjmarlow
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial