Jump to content

Factory pattern

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Tomtheeditor (talk | contribs) at 14:55, 24 May 2010 (Corrected spelling of Java.). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
See also Factory method pattern

The factory pattern is a creational design pattern used in software development to encapsulate the processes involved in the creation of objects.

The creation of an object often requires complex processes not appropriate to include within a composing object. The object's creation may lead to a significant duplication of code, may require information not accessible to the composing object, may not provide a sufficient level of abstraction, or may otherwise not be part of the composing object's concerns.

Some of the processes required in the creation of an object include determining which object to create, managing the lifetime of the object, and managing specialized build-up and tear-down concerns of the object.

Applicability

Use the factory pattern when:

  • The creation of the object precludes reuse without significantly duplicating code.
  • The creation of the object requires access to information or resources not appropriate to contain within the composing object.
  • The lifetime management of created objects needs to be centralised to ensure consistent behavior.

Example Implementations

Java

package accounting;

import java.lang.reflect.Constructor;

import java.util.HashMap;
public class ServicesFactory {
   private static   IObject interfaceObjectAdaptor;
   private static   String interfaceClassAdaptorString;
   private static   ServicesFactory factory;
   private static   HashMap<String,Constructor<?>> serviceAdapters;
   private static   HashMap<String,IObject> serviceObjects = new HashMap<String, IObject>();;
   private ServicesFactory( ){}
   //ExampleFactory (a singleton) 
   public synchronized static   ServicesFactory getServiceFactory(HashMap<String,Constructor<?>> factoryServices){
       if( factory == null ){
    	   serviceAdapters = factoryServices;
    	   factory = new ServicesFactory();    	   
       }
	   return factory;
   }
   public void setAdapterName(String cls ) {
	   this.interfaceClassAdaptorString = cls;
   }
   public IObject getAdaptor(){
	   String adapterName = this.interfaceClassAdaptorString;
	   try{
		   
	   if( this.interfaceObjectAdaptor == null){
		   Object obj = null;
		   Constructor<?> con = null;
		   Class<?> cl = System.class;
		   adapterName = System.getProperty(adapterName);

		   this.interfaceObjectAdaptor = (IObject)Class.forName(adapterName).newInstance();
	   }
	   
	   }catch(Exception e){}
	   return this.interfaceObjectAdaptor;
   }
   public boolean factoryItemExists(Constructor<?> cls){
	   if (ServicesFactory.serviceAdapters.containsValue(cls))
	      return true;
	   else
		   return false;
	   
   }
   public IObject getAdapter(String adapter){
		  //create a Service object, 
	   try{
	
		   Object obj = null;
		   Constructor<?> cl = null;
		   Class cls;
		   String str = null; 
		   cl = ServicesFactory.serviceAdapters.get(adapter);
		   obj = cl.getClass();
		   obj = serviceObjects.get(obj);
		   if( obj == null)
		   {
			   
			   obj = cl.newInstance();
			   serviceObjects.put(adapter,(IObject) obj);
		   }
		   
			   
		   
		   
	       
		 
		   return (IObject) obj;
	   }catch(Exception e){return null;}
	   }
  
}
package accounting;

import accounting.*;


import java.lang.reflect.Constructor;
import java.util.HashMap;


public class AccountingServices {
	private HashMap<String,Constructor<?>> services; 
	private ServicesFactory servicesFactory;
 public AccountingServices(){
	 try{
	  
	 

     

	 services = new HashMap<String, Constructor<?>>();
	
	 
     Class cls = accounting.SAPAdapter.class;
     Constructor<?> con = cls.getConstructor(null);
     con.setAccessible(true);
     services.put("SAPAdapter",con);
     cls = accounting.NorthernAccountingAdapter.class;
     con = cls.getConstructor(null);
     con.setAccessible(true);
     services.put("NorthernAccountingAdapter",con);
     
    
	 

     
	 	
	 
	 
	 
	 servicesFactory = ServicesFactory.getServiceFactory(services);
	 servicesFactory.setAdapterName("accountingfirm.class.name");
	 
	 }catch(Exception e){}
 }
 public IAccountingAdaptor getAccountingAdaptor(String cls){

	 return (IAccountingAdaptor)servicesFactory.getAdapter(cls);
	
	 
 }
 
 public IAccountingAdaptor getAccountingAdaptor(){

	 return (IAccountingAdaptor)servicesFactory.getAdaptor();
	
	 
 }
 public static void main(String args[]) {
	    IAccountingAdaptor myIface = null;
	    String adapter1 = "SAPAdapter";
	    String adapter2 = "NorthernAccountingAdapter";
		AccountingServices sb = new AccountingServices();
		myIface = sb.getAccountingAdaptor(adapter1);
		myIface.postReceivable("Factory Made");
		myIface.postSale("Factory Made");
		myIface = sb.getAccountingAdaptor(adapter2);
		myIface.postReceivable("Factory Made");
		myIface.postSale("Factory Made");
	    myIface = sb.getAccountingAdaptor();
	    myIface.postReceivable("Factory Made");
		myIface.postSale("Factory Made");
		}
}

PHP

class Factory
{
    public static function build($type)
    {
        $class = 'Format' . $type;
        if (!class_exists($class)) {
            throw new Exception('Missing format class.');
        }
        return new $class;
    }
}

class FormatString {}
class FormatNumber {}

try {
    $string = Factory::build('String');
}
catch (Exception $e) {
    echo $e->getMessage();
}

try {
    $number = Factory::build('Number');
}
catch (Exception $e) {
    echo $e->getMessage();
}

The abstract factory pattern is similar to the factory pattern, but has the added intent of providing an interface to a factory purposed for the creation of families of related objects in order to enable variations of the factory to be used interchangeably. The abstract factory pattern is a compound pattern incorporating the use of the factory pattern and the interface pattern.

The factory method pattern is also concerned with the encapsulation of object creation details, but has the intent of providing an interface which defers the creation of objects to subclasses. The factory method pattern is also a compound pattern which incorporates the use of the Factory pattern and the interface pattern.

The builder pattern is also concerned with the encapsulation of object creation details, but has the intent of abstracting the steps involved in the construction process as opposed to encapsulating the entire creation process in one step. The factory pattern may encapsulate use of the builder pattern internally as part of the object creation process.