Web site directory - 354 CHAPTER 11 REST-BASED MODEL VIEW CONTROLLER
354 CHAPTER 11 REST-BASED MODEL VIEW CONTROLLER PATTERN Amazon.com local client be instantiated without having a valid configuration. The use of passing configuration items by using the constructor would apply to the Google search engine client and any other local client. To wire the local clients to the controller, another method that implements the Builder pattern is used. An example of implementing the Builder pattern for the local clients is as follows: public class SearchBuilder { private static String _amazonEndPoint; private static String _googleEndPoint; private static String _amazonAccessKey; private static String _amazonSecretKey; private static String _googleAccessKey; private static boolean _didAssign = false; public static void assignConfiguration( String amazon, String amazonAccessKey, String amazonSecretKey, String google, String googleAccessKey) { _amazonEndPoint = amazon; _amazonAccessKey = amazonAccessKey; _amazonSecretKey = amazonSecretKey; _googleEndPoint = google; _googleAccessKey = googleAccessKey; if( _amazonEndPoint == null || _amazonEndPoint.length() == 0 || _googleEndPoint == null || _googleEndPoint.length() == 0 || _amazonAccessKey == null || _amazonAccessKey.length() == 0 || _amazonSecretKey == null || _amazonAccessKey.length() == 0 || _googleAccessKey == null || _googleAccessKey.length() == 0) { throw new IllegalStateException( “configuration data invalid”); } _didAssign = true; } public static void buildCommands( Parent parent) { if( ! _didAssign) { throw new IllegalStateException( “configuration data not assigned”); } parent.clearAllCommands(); parent.addCommand( new AmazonSearchCommand( _amazonEndPoint, _amazonAccessKey, _amazonSecretKey)); parent.addCommand( new GoogleSearchCommand( _googleEndPoint, _googleAccessKey)); } } The class SearchBuilder has two static methods: assignConfigurationand buildCommands. The method assignConfiguration assigns the default configuration to the Amazon.com or Google local clients when the local clients are instantiated. In the example, the configuration values are referenced as simple strings, but those strings could have been converted into types, and the method assignConfiguration could have referenced those types. Converting the strings would probably have been a good idea because five parameters can become a bit
In case you need quality webspace to host and run your web applications, try our personal web hosting services.