Practical web service design model, part 1

Source: Internet
Author: User
The second part of this series continues to discuss applications in the Web service field by introducing the command Facade Pattern, these applications are well-defined and comply with tested web application design policies.

In this series, I have discussed how to use Java messaging service to implement programming strategies for asynchronous programming models for Web Services. Here I continue to focus on the use of simple and proven design patterns in the Web service environment. My goal is to encode the service implementation by providing practical examples to you in a specific way.

Command Facade Pattern

The two modes that web application developers are familiar with are:FacadeAndCommandMode.Command FacadeThe model is the joint product of the two models and is specially designed for the service environment. Similarly, it includes the basic features of these two source modes, but the implementation method allows developers to understand the Implementation of Web service interfaces described in WSDL and accessible through soap messages.

The command mode is very famous. It encapsulates some unique activities into reusable objects. For each request, the behavior of these objects can be parameterized.

Figure 1. Command pattern

The command object may be stateful or stateless. A stateful Command maintains the internal status of its data and is specific to the variables used independently each time.Stateful command instanceIt is usually used only once, or is put into the pool, and is recycled after each client is used. When an object instance is created or a command is called by setting properties, the State command parameter is specified.Stateless command instanceIt does not contain the internal status, and multiple clients can be used concurrently without sharing or recycling. When the call method of this command is input, the stateless command parameter is assigned.

In the facade mode, independent advanced application components are used to serve encapsulated interoperability and simplify interaction with the system. In other words, facade can simplify complex interfaces (or even multiple subordinate components) between the client and the service provider ).

Figure 2. Facade Pattern

Like command, facades may be stateful or stateless. A stateful facade contains the internal status between multiple client calls. These client calls are used to call the operations defined in the facade. Stateless facade does not contain the internal memory of the previous operation, so it depends on the required State passed by each method call.

The command facade mode combines the two methods by introducing the facade interface located at the front of one or more command objects. The goal of this mode is to present a less complex and more friendly user interface to potential clients and complete business logic encapsulation like the command mode.

Figure 3. Command facade Mode

In the Web Service world, the facade component in command facade mode is described in WSDL by means of a one-to-one relationship between the methods defined in facade and the operations defined in Port type.portTypeAssociated. To perform specific operations, each method can call one or more encapsulated command objects in turn. Given a typical Java Web Service (a separate Java class in this Service supports a separate WSDLportType), The primary purpose of this model is to separate the business logic from the service implementation class and put it into specific business objects that are easier to manage and evolve over time.

Command implementation example

The command facade mode consists of two unique components:CommandAndFacade.FacadeDesign andCommandThere is a direct relationship, including each typeCommandVarious Parameter options. Therefore, the key to designing the command facade implementation is to design the command and define the appropriate facade interface from there.

This simple application example defines two very useful operations to convert input strings into uppercase or lowercase letters. These highly complex and accurate computing operations are encapsulated into two stateless command objects.

However, before you start, you need to define the basicCommandInterface.

Listing 1. Command. Java


package com.ibm.developerworks.wspattern.two;

public interface Command {
public CommandModel execute(CommandModel model);
}

CommandAn interface only defines a separate method calledexecuteThis method accepts a separate operation calledCommandModel.CommandModelIt is essentially a placeholder interface. The purpose of this specific command implementation is to allow the user of the command object to pass in parameters specific to this command implementation.

Listing 2. commandmodel. Java


package com.ibm.developerworks.wspattern.two;

public abstract class CommandModel {}

Because each business operation has a separate text string as the input, both operations use the sameCommandModelImplementation, as shown in listing 3.

Listing 3. casecommandmodel. Java


package com.ibm.developerworks.wspattern.two.commands;

import com.ibm.developerworks.wspattern.two.CommandModel;

public class CaseCommandModel
extends CommandModel {

private String string;

public String getString() {
return string;
}

public void setString(String string) {
this.string = string;
}
}

Listing 4. uppercasecommand. Java


package com.ibm.developerworks.wspattern.two.commands;

import com.ibm.developerworks.wspattern.two.Command;
import com.ibm.developerworks.wspattern.two.CommandModel;

public class UppercaseCommand
implements Command {

public CommandModel execute(CommandModel model) {
if (!(model instanceof CaseCommandModel)) {
throw new IllegalArgumentException("Invalid command model");
} else {
CaseCommandModel umodel = (CaseCommandModel)model;
if (umodel.getString() == null) {
throw new IllegalArgumentException("Invalid command model");
} else {
umodel.setString(umodel.getString().toUpperCase());
}
return umodel;
}
}
}

Listing 5. lowercasecommand. Java


package com.ibm.developerworks.wspattern.two.commands;

import com.ibm.developerworks.wspattern.two.Command;
import com.ibm.developerworks.wspattern.two.CommandModel;

public class LowercaseCommand
implements Command {

public CommandModel execute(CommandModel model) {
if (!(model instanceof CaseCommandModel)) {
throw new IllegalArgumentException("Invalid command model");
} else {
CaseCommandModel umodel = (CaseCommandModel)model;
if (umodel.getString() == null) {
throw new IllegalArgumentException("Invalid command model");
} else {
umodel.setString(umodel.getString().toLowerCase());
}
return umodel;
}
}
}

Note:LowercaseCommandAndUppercaseCommandBoth objects are not stored in the internal state. More appropriately, all the content to be executed by each command isCommandModelA part of the parameter is passed in. Also note that the execute method returnsCommandModel. Command may return anything it expectsCommandModel. In the example aboveCommandModelThe object is passed into the command and updated and returned to the called application. However, these actions are not required.

Implement Facade

Facade aims to simplify and centralize access to the underlying command object, making these commands more accessible as Web services described in WSDL. For this purposeFacadeSome methods are defined in the interface, allowing various Parameter options exposed by the underlying command to be passed into the facade method as parameters. InFacadeAn independent method in the interface can execute one or more command objects. Therefore, the facade input parameters and method return values must be well selected.

For this example, the facade interface exposes a public method for each command, as shown in Listing 6.

Listing 6. commandfacadeservice_sei.java


package com.ibm.developerworks.wspattern.two;

public interface CommandFacadeService_SEI extends java.rmi.Remote {
public java.lang.String toUpper(java.lang.String string);
public java.lang.String toLower(java.lang.String string);
}

The implementation of this interface includestoUpperAndtoLowerOperation.

Listing 7. commandfacadeservice. Java


package com.ibm.developerworks.wspattern.two;

import com.ibm.developerworks.wspattern.two.commands.LowercaseCommand;
import com.ibm.developerworks.wspattern.two.commands.UppercaseCommand;
import com.ibm.developerworks.wspattern.two.commands.CaseCommandModel;

public class CommandFacadeService {

private static final String CMD_TOUPPER = "toUpper";
private static final String CMD_TOLOWER = "toLower";

private static java.util.HashMap commands =
new java.util.HashMap();
static {
commands.put(CMD_TOUPPER, new UppercaseCommand());
commands.put(CMD_TOLOWER, new LowercaseCommand());
}

private static Command getCommand(String name) {
return (Command)commands.get(name);
}

public String toUpper(String string) {
CaseCommandModel model = new CaseCommandModel();
model.setString(string);
model = (CaseCommandModel)getCommand(CMD_TOUPPER).execute(model);
return model.getString();
}

public String toLower(String string) {
CaseCommandModel model = new CaseCommandModel();
model.setString(string);
model = (CaseCommandModel)getCommand(CMD_TOLOWER).execute(model);
return model.getString();
}
}

Note that becauseUppercaseCommandAndLowercaseCommandThe object is stateless, so every timetoUpperAndtoLowerWhen a method is called, you create a static cache for an instance instead of calling a new instance.

Once the facade interface has been implemented, the final step is to make it public to the outside as a web service. Assuming that this class is exposed as a JSR-109-compliant Web service, I strongly recommend that you use tools such as IBM WebSphere Studio application developer to generate all the deployment artifacts you need. To make it easier to use, I have included a downloadable ZIP file that contains all the source files (in the form of the application developer project) and an ear file, contains all compiled source files for the sample application. You can clickCodeTo download these files.

Encapsulation

The most important advantage of the command facade mode is that it allows the business logic behind Web Services to be encapsulated into specific objects, which are easier to manage and change over time. As long as the input and output of command implementation remain unchanged, changes in the business logic do not have to affect the Web service implementation layer. Furthermore, the command object allows you to reuse business logic in other fields of Web applications.

The examples provided in this article focus on stateless facade using stateless command. You can easily extend this model by using the mechanisms provided by HTTP sessions or defined in the Web service resource framework (WS-resource framework) to support the use of stateful facade, and some very interesting potential situations are allowed.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.