SDSU CS 635: Advanced Object-Oriented Design & Programming
Spring Semester, 1998
Motivating the Product Trader

To Lecture Notes Index
© 1998, All Rights Reserved, SDSU & Roger Whitney
San Diego State University -- This page last updated 21-Apr-98

Contents of Doc 13, Motivating the Product Trader


Reference slide # 1
Towards a Simplified Product Trader slide # 2
...When would we Use ProductTrader? slide # 14

Reference


Product Trader, by Baumer & Riehle in Pattern Languages of Program Design 3, Eds Martin, Riehle, Buschman, 1998, pp 29-46.

Doc 13, Motivating the Product Trader Slide # 2

Towards a Simplified Product Trader


     
class ApplicationClass     {
     AbstractWidget a;
     
     public appMethod1()     {
          AbstractWidget d = new Widget();
          d.widgetMethod1();
          AbstractWidget e = new Widget();
          blah;
     }

The "new Widget()" hard coded in the program can cause problems

Factory Method, Prototype, and Abstract Factory help remove this problem

What if we want to create both a Widget and a WidgetB in the Application Class and want to avoid hard coding "new Widget()" and "new WidgetB()" in our code?

Doc 13, Motivating the Product Trader Slide # 3
But First Things First!

What does it mean to say:

What if we want to create both a Widget and a WidgetB in the Application Class and want to avoid hard coding "new Widget()" and "new WidgetB()" in our code?

Where is the variation going to be?



Doc 13, Motivating the Product Trader Slide # 4
Solution 1. Two Factory Methods

class ApplicationClass     {
     AbstractWidget a;
     AbstractWidget b;
     
     // If C+ make this a virtual function and use pointers
     // to ApplicationClass object
     protected AbstractWidget createWidget() {
          return new Widget();
     }

     protected AbstractWidget createWidgetB() {
          return new WidgetB();
     }
          
     public appMethod1()     {
          AbstractWidget d = createWidget();
          d.widgetMethod1();
          blah;
          AbstractWidget e = createWidgetB();
          blah;
     }
     etc.
}

What have we gained?
What does it cost us?


Doc 13, Motivating the Product Trader Slide # 5
Solution 2. Use Java's forName

Java allows us to create an object from a string of the object's class

class Example
     {
     public String toString()
          {
          return "This is a simple class";
          }
     }

class Test
     {
     public  static  void  main( String  args[] ) 
          throws Exception
          {
          Class which = Class.forName( "Example" );
          Object whichOne = which.newInstance();
          System.out.println( whichOne.toString() );
          }
     }

Output
This is a simple class

Doc 13, Motivating the Product Trader Slide # 6
Solution 2. Use Java's forName part A

class ApplicationClass     {
     AbstractWidget a;
     AbstractWidget b;
     
     protected AbstractWidget createWidget(String type) {
          return Class.forName( type ).newInstance();
     }

               
     public appMethod1()     {
          AbstractWidget d = createWidget( "Widget");
          d.widgetMethod1();
          blah;
          AbstractWidget e = createWidgetB( "widgetB");
          blah;
     }
     etc.
}

What have we gained?
What does it cost us?


Doc 13, Motivating the Product Trader Slide # 7
Solution 2. Use Java's forName part B

class ApplicationClass     {
     String  widgetType;
     String  anotherWidgetType;
     
     public ApplicationClass( String typeA, String typeB ) {
          widgetType = typeA;
          anotherWidgetType = typeB;
     }

     protected AbstractWidget createWidget(String type) {
          return Class.forName( type ).newInstance();
     }

               
     public appMethod1()     {
          AbstractWidget d = createWidget(  widgetType );
          d.widgetMethod1();
          blah;
          AbstractWidget e = 
               createWidgetB( anotherWidgetType );
          blah;
     }
     etc.
}

What have we gained?
What does it cost us?

Doc 13, Motivating the Product Trader Slide # 8
Solution 3. Factory Method with parameters

class ApplicationClass     {
     String  widgetType;
     String  anotherWidgetType;
     
     public ApplicationClass( String typeA, String typeB ) {
          widgetType = typeA;
          anotherWidgetType = typeB;
     }

     protected AbstractWidget createWidget(String type) {
          if ( type.equals( "A" ) )
               return new Widget();

          else if ( type.equals( "B" ) )
               return Class.forName( type ).newInstance();
     }

               
     public appMethod1()     {
          AbstractWidget d = createWidget(  widgetType );
          d.widgetMethod1();
          blah;
          AbstractWidget e = 
               createWidgetB( anotherWidgetType );
          blah;
     }
     etc.
}

What have we gained?
What does it cost us?
Doc 13, Motivating the Product Trader Slide # 9
Specification, Creators and Maps

The string parameter of createWidget() on the previous slide is a specification

How to use the specification to create the desired object without using if statements or case statements?

Two Ways to avoid if Statements

A. Use a hash table ( which is just a map )

B. Polymorphism



Doc 13, Motivating the Product Trader Slide # 10
Hash Table

Use each possible specification as a key in the hash table

For the value stored at a key, place code that will create the desired widget, this code is called a creator

The following example assumes Widgets have a clone method, that is they are prototypes!

class ProductTrader     {
     Hashtable creators;

     public ProductTrader() {
          creators = new HashTable();
          mapSpecToCreators();
     }

     protected void mapSpecToCreators()  {
          creators.put( "A", new WidgetX() );
          creators.put( "B", new WidgetY() );
          creators.put( "C", new WidgetX() )
     }

     public AbstractWidget create( String aSpec ) {
          AbstractWidget copy =
               (AbstractWidget) creators.get( aSpec );
          return copy.clone();
     }
} 


We could extend the public interface to ProductTrader to allow run time setting of map from Specifications to Creators
Doc 13, Motivating the Product Trader Slide # 11
Now for using the ProductTrader

class ApplicationClass     {
     ProductTrader  myTrader;
     
     public ApplicationClass( ProductTrader aTrader) {
          myTrader = aTrader;
     }
               
     public appMethod1()     {
          AbstractWidget d = trader.create(  "B" );
          d.widgetMethod1();
          blah;
          AbstractWidget e =  trader.create(  "A" );;
          blah;
     }
     etc.
}


What have we gained?
What does it cost us?

Doc 13, Motivating the Product Trader Slide # 12
Polymorphism

For each possible specification create a class

Specification Classes

class ProductTrader     {
     AbstractWidget create( A aSpec ) {
          return new WidgetX();
     }
     
     AbstractWidget create( B aSpec ) {
          return new WidgetY();
     }

     AbstractWidget create( C aSpec ) {
          return new WidgetX();
     }
} 


Doc 13, Motivating the Product Trader Slide # 13
Now for using the ProductTrader

class ApplicationClass     {
     ProductTrader  myTrader;
     
     public ApplicationClass( ProductTrader aTrader) {
          myTrader = aTrader;
     }
               
     public appMethod1()     {
          AbstractWidget d = trader.create(  new B() );
          d.widgetMethod1();
          blah;
          AbstractWidget e =  trader.create( new A() );
          blah;
     }
     etc.
}


What have we gained?
What does it cost us?


Doc 13, Motivating the Product Trader Slide # 14

When would we Use ProductTrader?

Example 1 - Window Widget Layout

Goal
Create on-line surveys via html forms using a text description of the questions

A description of the type of input for each question should generate proper type of input field and the proper type of check for valid data

Also need to generate code to store answers in a data base and code to generate and display summary of the survey


Doc 13, Motivating the Product Trader Slide # 15
Possible Text Description of a Survey

<QUESTION On a scale of 1 - 10 how important is AZTEC Basketball to you? >
<ANSWER TYPE=integer START=1 END=10>

<QUESTION Describe your feelings toward the recent blue screen of death attack.>
<ANSWER TYPE=text >


To generate the html form for the survey we need to map strings like "integer", "float", "text" to the proper html tags

The tags we generate might differ for different browsers (XML has some useful features which are not supported by all browsers)

To process the answers we need to map the same strings to the proper range checks

The use of if or case statements would make it hard to add more types of answers later on


visitors since 05-Mar-98