SDSU CS 635: Advanced Object-Oriented Design & Programming
Spring Semester, 1998
Motivating Creational Patterns

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 11, Motivating Creational Patterns


References slide # 1
Motivating Creational Patterns slide # 2

References


Design Patterns: Elements of Reusable Object-Oriented Software, Gamma, Helm, Johnson, Vlissides, Addison Wesley, 1995, chapter 3

Doc 11, Motivating Creational Patterns Slide # 2

Motivating Creational Patterns




Assume that the ApplicationClass uses the Widget class in many places

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

   public appMethod2()   {
      blah;
      Widget f = new Widget();
      f.widgetMethod1();
      more blah;
      Widget g = new Widget();
      blah;
   }
   etc.
Doc 11, Motivating Creational Patterns Slide # 3
Variation
We can modify the internal Widget code without modifying the ApplicationClass

What happens when we discover a new widget and would like to use in the ApplicationClass?

Note this is a hokey professor example, in a reality variations of a Widget may be handled by parameters to a single Widget class, not by subclassing it.

Widget and ApplicationClass are coupled in several ways

ApplicationClass knows the Widget interface
ApplicationClass explicitly uses the Widget Type
ApplicationClass explicitly creates new Widgets in many locations

Doc 11, Motivating Creational Patterns Slide # 4
Apply "Program to an Interface"
class ApplicationClass   {
   AbstractWidget a;
   AbstractWidget b;
   
   public appMethod1()   {
      AbstractWidget d = new Widget();
      d.widgetMethod1();
      blah;
      AbstractWidget e = new Widget();
      blah;
  }

 etc.
}

But we still have hard coded which widget to create!
Doc 11, Motivating Creational Patterns Slide # 5
Solution 1 - Use a (Virtual ) Factory Method

class ApplicationClass   {
   AbstractWidget a;
   AbstractWidget b;
   
   // If C+ make this a virtual function and use pointers
   // to ApplicationClass object
   public AbstractWidget createWidget() {
      return new Widget();
   }
      
   public appMethod1()   {
      AbstractWidget d = createWidget();
      d.widgetMethod1();
      blah;
      AbstractWidget e = createWidget();
      blah;
   }
   etc.
}

class ApplicationClassB   extends ApplicationClass{

   public AbstractWidget createWidget() {
      return new WidgetB();
   }
}

Elsewhere:

ApplicationClass test = new ApplicationClassB();
test.appMethod1();

Doc 11, Motivating Creational Patterns Slide # 6
Solution 2 - Clone a Prototype

Provide the Widgets with a clone method that will make a copy of an existing Widget object



class Widget {
   int data;
   
   public Widget clone() {
      Widget aCopy = new Widget();
      aCopy.data = data;
      return aCopy;
   }
}

Using Clone method

Widget  original = new Widget();
Widget anIndependentCopy = original.clone();

Doc 11, Motivating Creational Patterns Slide # 7
Using the clone in ApplicationClass

class ApplicationClass   {
   AbstractWidget a;
   AbstractWidget b;
   AbstractWidget prototype;
   
   public ApplicationClass(AbstractWidget cloneMe ) {
      prototype = cloneMe;
   }
      
   public appMethod1()   {
      AbstractWidget d = prototype.clone();
      d.widgetMethod1();
      blah;
      AbstractWidget e = prototype.clone();
      blah;
   }

   etc.
}

Elsewhere:

ApplicationClass test = 
   new ApplicationClass( new Widget() );

ApplicationClass testB = 
   new ApplicationClass( new WidgetB() );

Doc 11, Motivating Creational Patterns Slide # 8
More Variation

What if ApplicationClass uses Widgets, Cogs, Wheels, Gaskets, Gears, Gismos, ThingABobs, and Gadgets

Each one of theses are separate classes and each one has subclasses

Further assume that there are restrictions on what type of Widget can be used with which type of Cog, etc.


Factory methods or Prototypes can handle Widgets, Cogs, Wheels, Gaskets, Gears, Gismos, ThingABobs, and Gadgets, but it get hard to insure the wrong types of items are not used together


Doc 11, Motivating Creational Patterns Slide # 9
A Solution:
Put all factory methods in a Factory class

Abstract Factory

class ApplicationClass   {
   AbstractWidget a;
   AbstractCog b;
   AbstractFactory partFactory;
   
   public ApplicationClass(AbstractFactory aFactory) {
      partFactory = aFactory;
   }
      
   public appMethod1()   {
      AbstractWidget d = partFactory.makeWidget();
      d.widgetMethod1();
      blah;
      AbstractCog e = partFactory.makeCog();
   } etc.

visitors since 26-Feb-98