HTML and CSS Reference
In-Depth Information
Step 1—Create the Component Model and Logic
When creating components it is tempting to mingle your component logic with the classes that must be implemented
to make it work on the framework. However, this makes the component difficult to test and you mix framework
specificities with logic that could be encapsulated and reused. It also makes it difficult to upgrade in case the
framework suddenly has new or different requirements on how to implement components.
The first step in component creation is to build the model and logic before getting into how the model and logic
will interface with the UIComponent and Renderer classes. The model for the RandomText component is rather simple.
It is a simple class called RandomTextAPI with a single method that invokes the RandomText REST service and returns
the output received from the service. Listing 6-1 show the source code of the simplified service.
Listing 6-1. RandomTextAPI.java Implementing a Simplified API for Obtaining Random Text from the Online
randomtext.me REST Service
package com.apress.projsf2html5.chapter6.components;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
/**
* Simple API for obtaining random text from an online REST service.
*
* @see <a href=" http://www.randomtext.me">RandomText</a >
*/
public class RandomTextAPI {
/**
* Enumeration containing the type of text to return.
*/
public enum TextType {
/** Return gibberish text. */
gibberish,
/** Return lorem ipsum text. */
lorem
};
/**
* Enumeration containing the type of formatting to return.
*/
 
Search WWH ::




Custom Search