118 CHAPTER 5 PERMUTATIONS PATTERN In the (Web site construction)
118 CHAPTER 5 PERMUTATIONS PATTERN In the example source code, the class Factory has a static method, Instantiate, that creates an instance of IBase by instantiating Implementation1. In the class method UseIt.Method, an instance of IBaseis instantiated by calling the method Factory.Instantiate. The class UseIt has no idea whether Implementation1 or Implementation2 is instantiated. The class UseIt uses the interface as defined by IBase and expects the interface methods to be implemented correctly. Those users of dynamic programming languages such as Ruby or Python do not implement interfaces. Dynamic programming languages use contracts where functionality is implied. Let s relate this to URLs and separate the resource from the representation. The resource is the interface, and the representation is the implementation. Right now most web technolo gies bind together resource and representation or use implementations directly, as the URLs http://mydomain.com/item.aspx and http://mydomain.com/item.jsp illustrate. The direct bindings are the extensions .aspx, and .jsp, and the proper interface-defined URL would have been http://mydomain.com/item. Ironically, all web technologies implement the separation of resource from representation for the root URL /, as illustrated by the following HTTP conversation. (Note that the conversa tion has been abbreviated for explanation purposes). Request: GET / HTTP/1.1 Host: 192.168.1.242:8100 User-Agent: Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; . en-US; rv:1.7.8) Gecko/20050511 Response: HTTP/1.1 200 OK Server: Apache/2.0.53 (Ubuntu) PHP/4.3.10-10ubuntu4 The requested URL is /, and it is returned by the server as index.html or index.jsp or index.php or even default.aspx. If web technologies are capable of separating the resource from the representation for the root URL, why can t they carry this throughout the entire web application? It is a truly puzzling question. The root URL implements the Permutations pattern, and many other URLs would implement the pattern, but the pattern does not need to be used everywhere, as illustrated in Figure 5-7. The URL /account[user] has two representations, HTML and XML. Which representation is returned depends on the preference of the client. The preference of the client is determined by the Accept header. Let s say that the client wants the HTML content. Contained within the HTML content is a link to the file details.aspx. If the URL were theoretically pure, the URL /account/[user]/details.aspxshould have been /account/[user]/details. However, in some situations being theoretically pure is the wrong approach. Just as with interface-driven development, you do not always reference interfaces. However, in the content of details.aspx, the resource-based URL /account/[user]/transactions is referenced. The resource-based URL is referenced by two representations: details.aspx and details.xml. When implementing the Permutations pattern, what you are implementing is interface- driven development for the Web. A resource is an interface, and the representations are implementations. The current batch of web technologies supports web application components, but their granularity is too coarse.
Visit our web design programs services for an affordable and reliable webhost to suit all your needs.