If I'm an object - how can I pass myself instantly after my construction.
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Chris_Adams
Posted On:   Tuesday, July 3, 2001 06:28 AM

Lets pretend I'm a class, User. When I am instantiated - I know the first thing I must do is call a login method on a class, Login, after I initialize of my attributes. I need to do this login myself (no one else should create me then send me).


How can I do this within my constructor? It seems that if my constructor does not complete - I cannot officially send myself yet- like:



			
Login mylogin = new Login;
mylogin.login(this);



So what other options do I have. Is there a pattern that addresses this?

Re: If I'm an object - how can I pass myself instantly after my construction.

Posted By:   joe_heath  
Posted On:   Tuesday, July 3, 2001 07:42 AM

The pattern you need is a Factory.
If the object can only be created using the factory you can create the object and then perform operations with it before returning it..




eg.




public class User {

//note only this package can create these

User() {

}

}



public class UserFactory {

public User getUser() {

User user = new User();

if (login(user))

return user;

else

return null;

}

}



Use:

User loggedUser = myUserFactory.getUser();



You might also want to make the UserFactory a singleton and hold a state..
About | Sitemap | Contact