Tech Kraft

Documentum, AWS, Java, Ruby on Rails, Linux, Windows, App Servers


Java: Abstract Factory Pattern

AbstractFactory Pattern is a sort of super factory pattern. In this example we will create a factory of objects where each factory creates items just as in the regular Factory pattern.

/*
 Class: ItemType.java
 */
 public interface ItemType{
   void doSomething();
 }

Item1.java

/*
 Class: Item1.java
 */
public class Item1 implements Factory{

@Override
public void doSomething(){
     System.out.println("My Item 1.");
   }
}

Item2.java

/*
 Class: Item2.java
 */
 public class Item2 implements Factory{

@Override
public void doSomething(){
     System.out.println("My Item 2.");
   }
}

Item3.java

/*
 Class: Item3.java
 */
public class Item3 implements Factory{

@Override
public void doSomething(){
      System.out.println("My Item 3.");
   }
}

Create another interface

 

ItemProperty.java

/*
Class: ItemProperty.java
*/

public interface ItemProperty{
    void fillProperty();
}

Create concrete classes:

public class myProperty1 implements ItemProperty{

   @Override
   public void fillProperty(){
       System.out.println("Inside myProperty1 fillProperty");
   }
}

More classes:

public class myProperty2 implements ItemProperty{

   @Override
   public void fillProperty(){
       System.out.println("Inside myProperty2 fillProperty");
   }
}

Create the Abstract Factory

public abstract class AbstractFactory{
     abstract ItemProperty getProperty (String someProperty);
     abstract ItemType getItemType (String someItemType);
}

 

ItemTypeFactory.java

/*

Class: ItemTypeFactory.java

*/
public class ItemTypeFactory extends AbstractFactory{
  public ItemType getItem(String itemType){
     it(itemType == null) {
        return null;
      }

      if(itemType.equalsIgnoreCase("Item1")){
         return new Item1();
      }else if(itemType.equalsIgnoreCase("Item2")){
         return new Item2();
      }else if(itemType.equalsIgnoreCase("Item3")){
         return new Item3();
      }
  return null;
  }
@Override
ItemProperty getProperty (String someProperty) {
      return null;
   }
}


Create the ItemProperty factory

ItemPropertyFactory.java

public class ItemPropertyFactory extends AbstractFactory { 

@Override
 public Shape getItemType(String itemType){ 

      return null;

   }

@Override
 Property2 getItemProperty(String ItemProperty) { 

      if(ItemProperty == null){
         return null;
} if(ItemProperty.equalsIgnoreCase("ONE")){ 
return new myProperty1();
 } else if(ItemProperty.equalsIgnoreCase("TWO")){ 
return new myProperty2();
 } else if(ItemProperty.equalsIgnoreCase("THREE")){ 
         return new myProperty3();
      }
      return null;
   }
} 

FactoryProducer.java

public class FactoryProducer{

   public static AbstractFactory getFactory (String choice) {

      if (choice.equalsIgnoreCase("Type"){
           return new ItemTypeFactory();

      }else if (choice.equalsIgnoreCase("Property"){
          return new ItemPropertyFactory();

      }
   }
}

AbstractFactoryPatternDemo

public class AbstractFactoryPatternDemo{

    public static void main(String[] args) {
      AbstractFactory itemtypeFactory = FactoryProducer.getFactory("Type");
      ItemType someItem1 = itemtypeFactory.getItem("Item1");
      someItem1.doSomething();
      ItemType someItem2 = itemtypeFactory.getItem("Item2");
      someItem2.doSomething();

      ItemType someItem3 = itemtypeFactory.getItem("Item3");
      someItem3.doSomething();

      AbstractFactory itempropertyfactory = FactoryProducer.getFactory("property");
      ItemProperty someProperty1 = itempropertyfactory.getItemProperty("ONE");
      someProperty1.fillProperty();
   }
}


Leave a comment

About Me

Senior Software Engineer professional with over 16 years of success with multiple open source technologies and various Content Management platforms and solutions.

Proven technical abilities through numerous projects involving enterprise web application design and development, application installation, configuration and support, and workflow and collaboration system designs.

  • Ability to learn new technologies and platforms quickly and apply them to the task at hand.
  • Excellent analytical skills, and strong communication and collaboration abilities.
  • Technical emphasis in including but not limited to Java, Ruby on Rails, Documentum and Alfresco
    in both Linux and Windows based environments

Newsletter