Bhadri Madapusi

Subscribe to Bhadri Madapusi: eMailAlertsEmail Alerts
Get Bhadri Madapusi: homepageHomepage mobileMobile rssRSS facebookFacebook twitterTwitter linkedinLinkedIn


Article

WebSphere Commerce Design Patterns - Part 2

Lending a uniform structure to design patterns

IBM's WebSphere Commerce product is a platform for developing and deploying value chain solutions from a consumer-centric online sales channel to a completely integrated, multi-tier demand chain. A variety of design patterns were used to develop the WebSphere Commerce framework. By understanding and using these high-level design patterns, you can write WebSphere Commerce apps that adhere to the WebSphere Commerce framework. Furthermore, these patterns will help you customize the out-of-the-box capabilities of WebSphere Commerce.

WebSphere Commerce design patterns fall into two main categories: command design patterns and display design patterns. In the first category are the Controller Command pattern, Task Command pattern, and Data Bean Command pattern. In the second are the Smart Data Bean pattern and Command Data Bean pattern.

In this two-part article, I discuss the design patterns in terms of the template described in the book Design Patterns - Elements of Reusable Object-Oriented Software by Erich Gamma et al. This template lends a uniform structure to design patterns, making them easier to learn, compare, and use. In the first part, published in the June issue, I covered the controller command and task command. In this part, I'll cover Data Bean command and Display pattern.

Data Bean Command
INTENT: Avoid coupling a data object with the logic used to process and populate the data into the data object by moving this logic into a separate object. Modify the data fetching, processing, and population tasks independent of the actual data object.

MOTIVATION: WebSphere Commerce recommends using data beans (see the display pattern category) to display the data to the client. These data beans are data objects with properties, their getters and setters (essentially Java beans). The logic to fetch, process, and populate data must not be embedded in the data objects. Embedding this logic will strongly couple the implementation details with the data object. If your business needs change over time, this strong coupling will restrict you from modifying how your implementation fetches, processes, and populates data. If you have strong coupling and want to modify the implementation, you may have to rewrite your data object and change the client to this new data object.

Searching a product catalog is an important commerce feature. To display the results of your catalog search, you may use a data object that whose properties are defined for search criteria and search output. If your search implementation is coupled with the data object, you'll be forced to modify the data object or create a new data object when your search algorithm changes. This can be avoided by having the implementation in a separate object that can be varied dynamically with your business environment.

Suppose SearchDataBean is the data object that contains the search criteria and search output. SearchDataBean won't implement the search logic; instead the logic will be implemented by SearchDataBeanCmdImpl. By changing the implementation class, we can modify the search logic. SearchDataBean maintains a reference to the interface that will be implemented by the implementation class. DataBeanManager gets this interface reference from SearchDataBean and instantiates an appropriate implementation with the help of the CommandFactory. Furthermore, DataBeanManager will pass in a reference of the data bean to the implementation class so the implementer can get the input criteria and set the result data in the bean. See Figure 1

APPLICABILITY: Use this pattern when all of the following conditions apply:
a. You want to avoid coupling the data object and the algorithm used to fetch, process, and populate the data object.
b. You anticipate a change to the population business logic.
c. The data population business logic may vary with store type.

STRUCTURE:

See Figure 2

See Figure 3

PARTICIPANTS:
DataBeanCommand: A Java interface that declares the methods for the following tasks: to execute the logic that populates data beans, to set input properties that will be used by population logic, and to set/get the data bean reference. This interface is provided by the WebSphere Commerce Server.

DataBeanCommandImpl: An abstract class that provides the default implementation to the methods declared in DataBeanCommand. This interface is provided by the WebSphere Commerce Server.

ADataBeanCmd: A Java interface that declares additional methods required in fetch, process and populate operation. It also provides the name of the default implementation class that implements the methods that are defined by this interface and the business task logic. The CommandFactory instantiates the default class if it fails to find an appropriate implementation class in the command registry.

ADataBeanCmdImpl: Provides implementation for data fetching, processing, and population.

CommandFactory: Instantiates an appropriate implementation of ADataBeanCmd based on store type. The appropriate implementation class is defined in the command registry.

DataBean: The data object that's populated using the Data Bean Command pattern. This contains a reference to the command interface that's used to populate its properties.

DataBeanManager: The client class that executes the appropriate Data Bean Command implementation for a data bean.

Figure 3 illustrates the collaboration between participants in the Data Bean Command pattern.

CONSEQUENCES: The consequences of using the Data Bean Command pattern are as follows:
a. The Data Bean Command interface and implementation are first-class objects that can be manipulated and extended to change existing data fetching, processing, and population logic to provide new logic.
b. Data fetching, processing, and population logic can be changed without affecting the data objects.
c. Data objects must be aware of the (Data Bean Command) interface name whose implementation will be used to populate it. This interface name can be set explicitly by the client at runtime.
d. Each store type can have different fetching, processing, and population logic.

IMPLEMENTATION:
a. To create a new Data Bean Command in WebSphere Commerce you must create a new Java interface that extends DataBeanCommand and provides an implementation class for this extended Java interface. The implementation class must extend the default Data Bean Command implementation, DataBeanCommandImpl class.
b. The data population logic must be in the performExecute method. The caller (DataBeanManager) is programmed to the Data Bean Command interface. So all the input properties must be defined in the data bean or passed using setRequestProperties.
c. Always instantiate a Data Bean Command using the CommandFactory and call the execute method to execute the business process. Never call the implementation class's performExecute method directly.
d. A data bean instance must be explicitly passed into the Data Bean Command implementation class.
e. As a best practice, always use the populateDataBeanCommand method to populate the input data from the data bean to the Data Bean Command properties.
f. New population logic and extensions to existing population can be performed either by creating a new implementation class for the existing Data Bean Command interface or extending the existing Data Bean Command implementation class. When you make extensions, make sure you call the parent class's performExecute method.
g. You can modify the population logic by setting a different interface name in the data bean with setCommandInterfaceName.

More Stories By Bhadri Madapusi

Bhadri Madapusi is a software developer working in the IBM Toronto Lab's Electronic Commerce Division. He earned his MSc in information systems from BITS, India, and his MSc in computer science from Queens University, Canada. Bhadri's interests include software modeling, design patterns, and data management.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.