CS 4773 Object Oriented Systems Factory Patterns


We have already talked briefly about the factory pattern.
A factory pattern is one that concerns itself with the creation of objects.
There may be several reasons not to create an object directly (with new). There are different types of factory patterns:


The abstract factory pattern
An Example: Choosing a look-and-feel for a windowing system.
Possibilities: rectangular objects or rounded corners.
We will consider only two kinds of widgets: buttons and scrollbars.
The Abstarct Factory:

public abstract class LookAndFeel {
   public abstract Button makeButton(String s);
   public abstract Scrollbar makeScrollbar(boolean horizontal);
}

public class RectangularLookAndFeel extends LookAndFeel {
   public Button makeButton(String s) {
      return new RectangularButton(s);
   }
   public Scrollbar make Screollbar(boolean horizontal) {
      return new RectangularScrollbar(horizontal);
   }
}

public class RoundedLookAndFeel extends LookAndFeel { 
   public Button makeButton(String s) { 
      return new RoundedButton(s); 
   }
   public Scrollbar make Screollbar(boolean horizontal) {
      return new RoundedScrollbar(horizontal);
   } 
} 
How we would use this:
public class GUI {
   public GUI (LookAndFeel laf) {
   }
}
Which Look-and-feel to use would be decided outside of GUI, perhaps with a configuration file.
The GUI would only call the methods defined in LookAndFeel and would not know which of the two types it was using.


The factory method pattern
Problem: We have two parallel hierarchies of derived classes. For a given member of the first hierarchy we need a corresponding member of the second.

Solution: Have the elements of the first hierarchy create the corresponding element of the second hierarchy.

Example: Collections

ArrayList x = new ArrayList();
PriorityQueue y = new PriorityQueue();
TreeSet z = new TreeSet();

Method 1: (using new)
Iterator xi = new Iterator(x);
Iterator yi = new Iterator(y);
Iterator zi = new Iterator(z);

Method 2: (The Factory Method)
Iterator xi = x.iterator();
Iterator yi = y.iterator();
Iterator zi = z.iterator();


The singleton pattern
Multple objects have access to a single object.

Method 1:
Create the singleton object in another class before any the the objects using it need access.
This is what we do in the editor for the error handler.
In this case they gain access by use of a static method.

Method 2: the singleton pattern
Have each object attempt to create the singleton object.
The first one actually does the creation.
The others just get access to the one already created.

Example:

public class MySingleton {

   private static MySingleton mySingleton = null;

   private MySingleton() {
      // rest of constructor here
      mySingleton = this;
   }

   public static synchronized MySingleton getIt() {
      if (mySingleton == null)
         new MySingleton();
      return mySingleton;
   }

}
What would happen if the getIt method were not synchronized?