CHAPTER 11 REST-BASED MODEL VIEW CONTROLLER PATTERN (Web servers)

CHAPTER 11 REST-BASED MODEL VIEW CONTROLLER PATTERN interface General { } interface Specialization { public void Method(); } class Implementation implements General, Specialization { public Implementation( String extraInfo) { } public void Method() { } } class Factory { public static General CreateInstance( String extraInfo) { return new Implementation( extraInfo); } } The interface General is a minimal interface, a sort of placeholder. In the example, the General interface has no methods, but there could have been methods. The aim is to keep methods and property declarations to a minimum. The other interface, Specialization, has a single method, but is an interface used to specialize or provide a specific functionality not offered by the General interface. Where the Extension pattern comes into play is when the class Implementationimplements both General and Specialization. A user of Implementation would see the General interface, but could carry out a typecast that converts General into Specialization, as illustrated by the following source code: Specialization specialized = (Specialization)genericInstance; Notice that a typecast was made from one interface to another interface, and not to the implementation type Implementation. This is the essence of the Extension pattern, where interface instances are typecast to the required interface, assuming that the interface instance implements all of the required interfaces. By using the Extension pattern, a framework can deal with objects generically, and then by using typecasting can ask for specialized functionality. You might ask, Why not just pass around the type Object, because Object is very generic and after all, you are typecasting, and typecasting an Object is easy. Passing around Object is not suitable because Object is too generic. Even though the Generic interface had no methods, it is still a type that indicates whoever implements Generic does realize that there are other interfaces that could be implemented as well. Using Object says that any object can be stored, even an object that has absolutely nothing to do with the problem being solved. Although I ve said that you do not typecast to Implementation, but to an interface, there are occasions when typecasting to Implementation would be acceptable. For example, sometimes it would be silly to implement an interface for the sake of implementing an interface, because the derived type would be used only in a single solution domain space. What s more, that scenario will be illustrated by the types SearchResult and SearchRequest. With the advent of Java 1.5 and .NET 2.0, another programming technique called generics is available. Generics, in conjunction with constraints, could very well be used to implement the Extension pattern, but it is beyond the scope of this chapter. Those interested in further
We recommend cheap and reliable webhost to host and run your web applications: Coldfusion Web Hosting services.

Leave a Reply