The Model-View-Controller Pattern


The Factory Pattern

The Factory pattern is used to create classes whose purpose is to create and return objects or references to objects. Two well known Factory patterns include the Abstract Factory and the Factory Method . You’ve seen the Abstract Factory pattern in action in chapter 22 with the creation of the IEmployeeFactory interface and the EmployeeFactory class. Factory implementations are often Singletons as well.

Let’s take a look once more at the IEmployeeFactory interface shown in example 25.2.

Example 25.2: IEmployeeFactory.java

image from book
 1     package com.pulpfreepress.model; 2 3     public interface IEmployeeFactory { 4       IEmployee getNewSalariedEmployee(String f_name, String m_name, String l_name, int dob_year, 5                              int dob_month, int dob_day, String gender, String employee_number); 6       IEmployee getNewHourlyEmployee(String f_name, String m_name, String l_name, int dob_year, 7                              int dob_month, int dob_day, String gender, String employee_number); 8     }
image from book

Each IEmployeeFactory method returns a reference to an object of type IEmployee, which in this case is either a SalariedEmployee or an HourlyEmployee. Each of IEmployee’s methods are an example of a Factory Method.

A limitation of IEmployeeFactory and any classes that implement this interface is that the factory methods, by their very name, place a conceptual limit on the type of objects that will be created. Two different implementations of the getNewSalariedEmployee() method may, in two different concrete implementations of IEmployeeFactory, create different flavors of salaried employees. However, from a polymorphic standpoint, it should make no difference, so long as whatever comes our way implements the IEmployee interface. Still, the method names place a conceptual limit on the sort of IEmployee objects we expect from an IEmployeeFactory.

The Dynamic Factory

In many programming situations it would be nice to simply name the type of object we need, send that name to a factory, and have the factory make us an object of that type. You can do this with the Dynamic Factory. The Dynamic Factory combines the Factory pattern with dynamic class loading to achieve a flexible mechanism for object creation. The general approach to creating a Dynamic Factory is to create a class that contains a public method that takes a String argument representing the class name of the type of object you need to create. The method will then try and dynamically load the class into the JVM using the Class.forName() method and, if successful, create an object of that type and return its reference. The objects a Dynamic Factory creates must have default (i.e., no argument) constructors. Example 25.3 gives a simple example of a Dynamic Factory named InterfaceTypeFactory.

Example 25.3: InterfaceTypeFactory.java

image from book
 1     public class InterfaceTypeFactory { 2 3       public static InterfaceType newObjectByClassName(String classname) { 4         Object o = null; 5         try{ 6         Class c = Class.forName(classname); 7         o = c.newInstance(); 8         }catch(Exception e){ 9             System.out.println("Problem loading class or creating instance!"); 10            } 11        return (InterfaceType)o; 12      } 13    } // end InterfaceTypeFactory class definition
image from book

Referring to example 25.3 — the InterfaceTypeFactory class has one public static method named newObjectByClassName(String classname). It takes a String argument representing the fully-qualified class name of the object to be created and returns a reference of type InterfaceType. For the method to work the class requested must exist and be in the system’s classpath.

The following examples give the code for the InterfaceType interface and several classes that implement the interface. These are followed by a short program showing the InterfaceTypeFactory class in action. Figure 25-1 shows the results of running the MainTestApp program.

Example 25.4: InterfaceType.java

image from book
 1    public interface InterfaceType { 2      public String getMessage(); 3    }
image from book

Example 25.5: ClassA.java

image from book
 1     public class ClassA implements InterfaceType { 2       private String message = "ClassA's message"; 3       public String getMessage(){ return message; } 4     }
image from book

Example 25.6: ClassB.java

image from book
 1     public class ClassB implements InterfaceType { 2       private String message = "ClassB's message"; 3       public String getMessage(){ return message; } 4     }
image from book

Example 25.7: ClassC.java

image from book
 1     public class ClassC implements InterfaceType { 2       private String message = "ClassC's message"; 3       public String getMessage(){ return message; } 4     }
image from book

Example 25.8: MainTestApp.java

image from book
 1     public class MainTestApp { 2       public static void main(String[] args){ 3         InterfaceType t1 = InterfaceTypeFactory.newObjectByClassName("ClassA"); 4         InterfaceType t2 = InterfaceTypeFactory.newObjectByClassName("ClassB"); 5         InterfaceType t3 = InterfaceTypeFactory.newObjectByClassName("ClassC"); 6 7         System.out.println(t1.getMessage()); 8         System.out.println(t2.getMessage()); 9         System.out.println(t3.getMessage()); 10      } 11    }
image from book

image from book
Figure 25-1: Results of Running Example 25.8

Advantages Of The Dynamic Factory Pattern

One of the primary advantages of the Dynamic Factory pattern is that certain enhancements to an application that uses a dynamic factory can be made and implemented without the need to shut down the application. This can be done my making the necessary changes to any of the classes that are dynamically loaded and dropping them into the classpath as an upgrade to the previous version of that class. The next time the class is dynamically loaded the change will be effective. You will also have to structure your application architecture in such a way as to limit the number of outstanding references to the old version of the class otherwise the change will not propagate completely through the application.

To achieve this type of dynamic class loading behavior, the Dynamic Factory pattern must be used in conjunction with a properties file. Although it appears from looking at example 25.3 that the classes are being loaded each time the newObjectByClassName() method is called they are not. This is because the system class loader will not reload a class having the same name once it has been loaded into the JVM.

Quick Review

The Factory pattern is used to create classes whose purpose is to create objects of a specified type. The Dynamic Factory can be used to create object’s via Java’s dynamic class loading mechanism. One of the primary advantages of the Dynamic Factory pattern is that certain enhancements to an application that uses a dynamic factory can be made and implemented without the need to shut down the application.




Java For Artists(c) The Art, Philosophy, and Science of Object-Oriented Programming
Java For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504052
EAN: 2147483647
Year: 2007
Pages: 452

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net