Design Pattern - Factory Method

Design Paterns, Java, OOP Add comments

Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.

uml diagram

The classes and/or objects participating in this pattern are:

  • Product (Page)
      defines the interface of objects the factory method creates
  • ConcreteProduct (SkillsPage, EducationPage, ExperiencePage)
      implements the Product interface
  • Creator (Document)
      declares the factory method, which returns an object of type Product. Creator may also define a default implementation of the factory method that returns a default ConcreteProduct object.
      may call the factory method to create a Product object.
  • ConcreteCreator (Report, Resume)
      overrides the factory method to return an instance of a ConcreteProduct.

code:

// “Product”

abstract class Page
  {
  }

“ConcreteProduct”

class SkillsPage extends Page
  {
  }

“ConcreteProduct”

class EducationPage extends Page
  {
  }

“ConcreteProduct”

class ExperiencePage extends Page
  {
  }

“ConcreteProduct”

class IntroductionPage extends Page
  {
  }

“ConcreteProduct”

class ResultsPage extends Page
  {
  }

“ConcreteProduct”

class ConclusionPage extends Page
  {
  }

“ConcreteProduct”

class SummaryPage extends Page
  {
  }

“ConcreteProduct”

class BibliographyPage extends Page
  {
  }

“Creator”

import java.util.*;

public abstract class Document {
    private ArrayList pages = new ArrayList();

    // Constructor calls abstract Factory method
    public Document()
    {
      this.CreatePages();
    }

    public ArrayList GetPages()
    {
      return this.pages;
    }
    public void AddPages(Page page)
    {
      this.pages.add(page);
    }    

    // Factory Method
    public abstract void CreatePages();

}

“ConcreteCreator”

public class Resume extends Document {

    // Factory Method implementation
    public void CreatePages()
    {
    	AddPages(new IntroductionPage());
    	AddPages(new ResultsPage());
    	AddPages(new ConclusionPage());
    	AddPages(new SummaryPage());
    	AddPages(new BibliographyPage());
    }
}

“ConcreteCreator”

public class Report extends Document {

    // Factory Method implementation
    public void CreatePages()
    {
    	AddPages(new SkillsPage());
    	AddPages(new EducationPage());
    	AddPages(new ExperiencePage());
    }
}

and here goes client which use all of this above:

public class Client {

	public static void main(String[] args)
	{
	      // Note: constructors call Factory Method
	      Document[] documents = new Document[2];
	      documents[0] = new Resume();
	      documents[1] = new Report();

	      // Display document pages
	      for (Document document : documents)
	      {
	        System.out.println("\n" + document.getClass().getName() + "--");
	        for (Page page : document.GetPages())
	        {
	          System.out.println(" " + page.getClass().getName());
	        }
	      }

	}
}

this is program output:

Resume ——-
SkillsPage
EducationPage
ExperiencePage
Report ——-
IntroductionPage
ResultsPage
ConclusionPage
SummaryPage
BibliographyPage

Leave a Reply

You must be logged in to post a comment.

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in