CS 635: Advanced Object-Oriented Design & Programming |
---|
Reference | slide # 1 |
Towards a Simplified Product Trader | slide # 2 |
...When would we Use ProductTrader? | slide # 14 |
class ApplicationClass { AbstractWidget a; public appMethod1() { AbstractWidget d = new Widget(); d.widgetMethod1(); AbstractWidget e = new Widget(); blah; }
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?
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() ); } }
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. }
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. }
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. }
How to use the specification to create the desired object without using if statements or case statements?
B. Polymorphism
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(); } }
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. }
class ProductTrader { AbstractWidget create( A aSpec ) { return new WidgetX(); } AbstractWidget create( B aSpec ) { return new WidgetY(); } AbstractWidget create( C aSpec ) { return new WidgetX(); } }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. }
<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