We have a spring MVC based web application. Now we need to modify this application so that it renders properly on the smartphones.
For this we are going to create separate JSP's for the smartphones. So, ones the request comes from the browser we will check if the request is coming from desktop then we will show the normal JSP or if the request is coming from mobile then we will show JSP's for smartphones.
We will be using spring Mobile for this.
In some cases we will also want to restrict the data on the smartphones. We may not show all the data in the JSP's.
e.g. We may need to show only few items in the menu. Desktop web application will show full menu while smartphones will show less menu items. Even though we will have different JSP for desktop menu and mobile menu, Menu items are coming from the database.
Is there any design pattern which will help us in this? We dont want to write those if else conditions to check for device type.
Explanation: Command pattern is a data driven design pattern.
The Transfer Object pattern is used when we want to pass data with multiple attributes in one shot from client to server.
Factory Method is a creational design pattern that provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created.
if you see the menu problem from UI point of view, then you could use Abstract Factory design pattern. in general, you would have one common interface that produces menus:
interface MenuFactory {
Object createMainMenu();
Object createSomeOtherMenu();
}
and two implementations:
public class DesktopAppMenuFactory implements MenuFactory {
public Object createMainMenu() {
ask dao for menus intended for desktop variant
return ...
}
public Object createSomeOtherMenu() {
ask dao for menus intended for desktop variant
return ...
}
}
public class MobileAppMenuFactory implements MenuFactory {
public Object createMainMenu() {
ask dao for menus intended for mobile variant
return ...
}
public Object createSomeOtherMenu() {
ask dao for menus intended for mobile variant
return ...
}
}
then write a method that will create appropriate factory given client type:
public static MenuFactory createMenuFactory(String clientType) {
if( clientType is desktop.. ) {
return new DesktopAppMenuFactory();
} else if( clientType is mobile.. ) {
return new MobileAppMenuFactory();
}
}
and use MenuFactory in your controllers and JSPs without bothering which variant it is. this one the only if-statements are in the ebove utility method createMenuFactory()
.
on the other hand, if you see the problem from data point of view, then Strategy Pattern applied to service layer would be appropriate. but the code code would end up very similar to the above with *Factory
renamed to *Service
and implementations being called strategies rather than factories.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With