Home > Articles > Software Development & Management > Object Technology

  • Print
  • + Share This
This chapter is from the book

An E-Business Example

It's sometimes hard to convince a decision maker, who may have no development background, of the monetary savings of code reuse. However, when reusing code, it is pretty easy to understand the advantage to the bottom line. In this section, we'll walk through a simple but practical example of how to create a workable framework using inheritance, abstract classes, interfaces and composition.

An E-Business Problem

Perhaps the best way to understand the power of reuse is to present an example of how you would reuse code. In this example, we'll use inheritance (via interfaces and abstract classes) and composition. Our goal is to create a framework that will make code reuse a reality, reduce coding time, and reduce maintenance—all the typical software development wish-list items.

Let's start our own Internet business. Let's assume that we have a client, a small pizza shop called Papa's Pizza. Despite the fact that it is a small, family-owned business, Papa realizes that a Web presence can help the business in many ways. Papa wants his customers to access his website, find out what Papa's Pizza is all about, and order pizzas right from the comfort of their browsers.

At the site we develop, customers will be able to access the website, select the products they want to order, and select a delivery option and time for delivery. They can eat their food at the restaurant, pick up the order, or have the order delivered. For example, a customer decides at 3:00 that he wants to order a pizza dinner (with salads, breadsticks, and drinks), to be delivered to his home at 6:00. Let's say the customer is at work (on a break, of course). He gets on the Web and selects the pizzas, including size, toppings, and crust; the salads, including dressings; breadsticks; and drinks. He chooses the delivery option, and requests that the food be delivered to his home at 6:00. Then he pays for the order by credit card, gets a confirmation number, and exits. Within a few minutes he gets an email confirmation as well. We will set up accounts so that when people bring up the site, they will get a greeting reminding them of who they are, what their favorite pizza is, and what new pizzas have been created this week.

When the software system is finally delivered, it is deemed a total success. For the next several weeks, Papa's customers happily order pizzas and other food and drinks over the Internet. During this rollout period, Papa's brother-in-law, who owns a donut shop called Dad's Donuts, pays Papa a visit. Papa shows Dad the system, and Dad falls in love with it. The next day, Dad calls our company and asks us to develop a Web-based system for his donut shop. This is great, and exactly what we had hoped for. Now, how can we leverage the code that we used for the pizza shop in the system for the donut shop?

And how many more small businesses, besides Papa's Pizza and Dad's Donuts, could take advantage of our framework to get on the Web? If we can develop a good, solid framework, then we will be able to efficiently deliver Web-based systems at lower costs than we were able to do before. There will also be an added advantage that the code will have been tested and implemented previously, so debugging and maintenance should be greatly reduced.

The Non-Reuse Approach

There are many reasons the concept of code reuse has not been as successful as some software developers would like. First, many times reuse is not even considered when developing a system. Second, even when reuse is entered into the equation, the issues of schedule constraints, limited resources, and budgetary concerns often short-circuit the best intentions.

In many instances, code ends up highly coupled to the specific application for which it was written. This means that the code within the application is highly dependent on other code within the same application.

A lot of code reuse is the result of simply using cut, copy, and paste operations. While one application is open in a text editor, you would copy code and then paste it into another application. Sometimes certain functions or routines can be used without any change. As is unfortunately often the case, even though most of the code may remain identical, a small bit of code must change to work in a specific application.

For example, consider two totally separate applications, as represented by the UML diagram in Figure 8.6.

Figure 8.6

Figure 8.6 Applications on divergent paths.

In this example, the applications testDonutShop and testPizzaShop are totally independent code modules. The code is kept totally separate, and there is no interaction between the modules. However, these applications might use some common code. In fact, some code might have been copied verbatim from one application to another. At some point, someone involved with the project might decide to create a library of these shared pieces of code to use in these and other applications. In many well-run and disciplined projects, this approach works well. Coding standards, configuration management, change management, and so on are all very well run. However, in many instances, this discipline breaks down.

Anyone who is familiar with the software development process knows that when bugs crop up and time is of the essence, there is the temptation to put some fixes or additions into a system that are specific to the application currently in distress. This might fix the problem for the distressed application, but could have unintended, possibly harmful, implications for other applications. Thus, in situations like these, the initially shared code can diverge, and separate code bases must be maintained.

For example, one day Papa's website crashes. He calls us in a panic, and one of our developers is able to track down the problem. The developer fixes the problem, knowing that the fix works but is not quite sure why. The developer also does not know what other areas of the system the fix might inadvertently affect. So the developer makes a copy of the code, strictly for use in the Papa's Pizza system. This is affectionately named Version 2.01papa. Because the developer does not yet totally understand the problem and because Dad's system is working fine, the code is not migrated to the donut shop's system.

The next week Dad calls up in a panic, with a totally unrelated problem. A developer fixes it, again not knowing how the fix will affect the rest of the system, makes a separate copy of the code, and calls it Version 2.03dad. This scenario gets played out for all the sites we now have in operation. There are now a dozen or more copies of the code, with various versions for the various sites. This becomes a mess. We have multiple code paths and have crossed the point of no return. We can never merge them again. (Perhaps we could, but from a business perspective, this would be costly.)

Our goal is to avoid the mess of the previous example. Although many systems must deal with legacy issues, fortunately for us, the pizza and donut applications are brand-new systems. Thus, we can use a bit of foresight and design this system in a reusable manner. In this way, we will not run into the maintenance nightmare just described. What we want to do is factor out as much commonality as possible. In our design, we will focus on all the common business functions that exist in a Web-based application. Instead of having multiple application classes like testPizzaShop and testDonutShop, we can create a design that has a class called Shop that all the applications will use.

Notice that testPizzaShop and testDonutShop have similar interfaces, getInventory and buyInventory. We will factor out this commonality and require that all applications that conform to our Shop framework implement getInventory and buyInventory methods. This requirement to conform to a standard is sometimes called a contract. By explicitly setting forth a contract of services, you isolate the code from a single implementation. In Java, you can implement a contract by using an interface or an abstract class. Let's explore how this is accomplished.

An E-Business Solution

Now let's show how to use a contract to factor out some of the commonality of these systems. In this case, we will create an abstract class to factor out some of the implementation, and an interface (our familiar Nameable) to factor out some behavior.

Our goal is to provide customized versions of our Web application, with the following features:

  • An interface, called Nameable, which is part of the contract.
  • An abstract class called Shop, which is also part of the contract.
  • A class called CustList, which we use in composition.
  • A new implementation of Shop for each customer we service.

The UML Object Model

The newly created Shop class is where the functionality is factored out. Notice in Figure 8.7 that the methods getInventory and buyInventory have been moved up the hierarchy tree from DonutShop and PizzaShop to the abstract class Shop. Now, whenever we want to provide a new, customized version of Shop, we simply plug in a new implementation of Shop (such as a grocery shop). Shop is the contract that the implementations must abide by:

public abstract class Shop  {

    CustList customerList;

    public void CalculateSaleTax() {

        System.out.println("Calculate Sales Tax");


    public abstract String[] getInventory();

    public abstract void buyInventory(String item);

Figure 8.7

Figure 8.7 A UML diagram of the Shop system.

To show how composition fits into this picture, the Shop class has a customer list. Thus, the class CustList is contained within Shop:

public class CustList {

    String name;

    public String findCust() {return name;}
    public void addCust(String Name){}


To illustrate the use of an interface in this example, an interface called Nameable is defined:

public interface Nameable {

    public abstract String getName();
    public abstract void setName(String name);


We could potentially have a large number of different implementations, but all the rest of the code (the application) is the same. In this small example, the code savings might not look like a lot. But in a large, real-world application, the code savings is significant. Let's take a look at the donut shop implementation:

public class DonutShop extends Shop implements Nameable {

    String companyName;

    String[] menuItems = {

    public String[] getInventory() {

        return menuItems;


    public void buyInventory(String item) {

        System.out.println("\nYou have just purchased " + item);


    public String getName(){

        return companyName;

    public void setName(String name){

        companyName = name;

The pizza shop implementation looks very similar:

public class PizzaShop extends Shop implements Nameable {

    String companyName;

    String[] foodOfferings = {
        "Garden Salad",

    public String[] getInventory() {

        return foodOfferings;


    public void buyInventory(String item) {

        System.out.println("\nYou have just purchased " + item);


    public String getName(){

        return companyName;

    public void setName(String name){

        companyName = name;


Unlike the initial case, where there are a large number of customized applications, we now have only a single primary class (Shop) and various customized classes (PizzaShop, DonutShop). There is no coupling between the application and any of the customized classes. The only thing the application is coupled to is the contract (Shop). The contract specifies that any implementation of Shop must provide an implementation for two methods, getInventory and buyInventory. It also must provide an implementation for getName and setName that relates to the interface Nameable that is implemented.

Although this solution solves the problem of highly coupled implementations, we still have the problem of deciding which implementation to use. With the current strategy, we would still have to have separate applications. In essence, you have to provide one application for each Shop implementation. Even though we are using the Shop contract, we still have the same situation as before we used the contract:

DonutShop myShop= new DonutShop();

PizzaShop myShop = new PizzaShop ();

How do we get around this problem? We can create objects dynamically. In Java, we can use code like this:

String className = args[0];

Shop myShop;

myShop = (Shop)Class.forName(className).newInstance();

In this case, you set className by passing a parameter to the code. (There are other ways to set className, such as by using a system property.)

Let's look at Shop using this approach. (Note that there is no exception handling and nothing else besides object instantiation.)

class TestShop {

   public static void main (String args[]) {

      Shop shop = null;

      String className = args[0];

      System.out.println("Instantiate the class:" + className + "\n");

      try {

       // new pizzaShop();
         shop = (Shop)Class.forName(className).newInstance();

      } catch (Exception e) {


      String[] inventory = shop.getInventory();

      // list the inventory

      for (int i=0; i<inventory.length; i++) {
         System.out.println("Argument" + i + " = " + inventory[i]);

      // buy an item




In this way, we can use the same application code for both PizzaShop and DonutShop. If we add a GroceryShop application, we only have to provide the implementation and the appropriate string to the main application. No application code needs to change.

  • + Share This
  • 🔖 Save To Your Account