开发者

Circular dependency between two methods in a class

开发者 https://www.devze.com 2023-03-25 20:08 出处:网络
I have a library class which contains two methods, say, Login() and NavigateToPage(). Now, in order to navigate to a page, the session has to be logged in. Also, to log in, one needs to first navigate

I have a library class which contains two methods, say, Login() and NavigateToPage(). Now, in order to navigate to a page, the session has to be logged in. Also, to log in, one needs to first navigate to the login page. As of now, my workaround is :

Login()
{
   NavigateToPage(LoginPage);
   // log in and do validation stuff.
   // set IsLoggedIn to true for further methods (also for NavigateToPage() method.)
   IsLoggedIn = true;
}

NavigateToPage (PageType pageType)
{
    if (pageType == LoginPage)
    {
       // navigate to login page.
       return;
    }
    if (! IsLoggedIn) Login();

    // switch case for navigation to other page types.
}

This is sort of a psuedocode. The actual code has worked till now, without any problems. Still, I feel there is something wrong as it looks cyclic. I think there is something that can be done to improve the code and remove the cyclic dependency. 开发者_高级运维Could someone suggest anything?

I had pretty much intended to make the question language and platform-agnostic. But I see that I couldnt explain it. Basically, the pages are independent of my class. The library that I am developing, takes a website and navigates its pages. I am just trying to automate the tasks in a website. So, the login page and other pages that I am talking about are in context of that website that I want to browse. The library automates that browsing. I hope I am clear now.


Using your existing pseudo logic, I will just refactor the code and introduce an explicit method for navigating to login page

Login()
{
   NavigateToLoginPage();
}
NavigateToLoginPage()
{
   // navigate to login page.
   // log in and do validation stuff.
   // set IsLoggedIn to true for further methods 
   IsLoggedIn = true;
}
NavigateToPage (PageType pageType)
{
    if (! IsLoggedIn) NavigateToLoginPage();

    // switch case for navigation to other page types.
}


You Have not mentioned the technology stack at all, but its defacto to have a seperate login page and main pages. And i am not sure if NavigateToPage(loginPage) makes sense as it is necessary to have a default page for any application which normally is the login page.


In your example, the login method should not have to redirect to the login page! Since this method will probably be called when the user hit submit IN the login page.

The login() method should only validate user/passwd and set the Session/http header with id of your choice to let the server knows that a valid session is ON. (with this, no more cycle ;-))

The NavigateToPage() could be renamed validateUserSession() and only verify if the user is loged on. If you use Java, the validateUserSession() should be in a servletFilter, so each time a request come to the server, the validation will be done.

Hope it help

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号