1
if ( accountType = "admin" )
        return admin view
if ( accountType = "customer" )
        return customer view
if ( accountType = "user" )
        return user view
if ( accountType = "merketing" )
        return marketing view

and so on .......

WeMakeSoftware
  • 9,039
  • 5
  • 34
  • 52

3 Answers3

1

Use a map where you register your views with the given key:

viewsMap.put("admin", admin_view);
viewsMap.put("customer", customer_view);
...

and then, just do:

View view = viewsMap.get(accountType);
if (view == null) {
    return error_view;
}
return view;
cello
  • 5,356
  • 3
  • 23
  • 28
  • still there will be processing in maps i want one line solution and want open closed principal – Arslan Khan Nov 14 '14 at 10:08
  • 1
    I don't think it can be done in one line, kind of matching the value of accountType to a variable name or something like this. Using a map is the closest you can get: one line to retrieve it (if you do not do any error checking), and one line for each view to register. – cello Nov 14 '14 at 10:10
  • i am kinda thinking putting it in DB then retrieve it concat it with base url. like redirectTo (baseURL + role + ".jsp"); – Arslan Khan Nov 14 '14 at 10:34
  • probably you should make your pseudo-code in the question a bit clearer. What do you want to return? a (complex?) view object, or just a String, for example a URL? It's really not clear from your question, especially as you don't say if and what framework you use. If it's just a string, I think Foolish had a nice solution he might post again. – cello Nov 14 '14 at 10:40
1

You can put it into HashMap and then just get it from there ;)

Maksym
  • 4,434
  • 4
  • 27
  • 46
0

If you want to separate the creation code, use a simplified factory:

class ViewFactory {
   public static View createView(String viewName)
   {
        if( viewName = admin )
            return new AdminView();

   }
}

You can do this is your views implement a common interface.

Matei Florescu
  • 1,155
  • 11
  • 23