Factory Method - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Factory Method

Description:

... (2,1,5,9,Color.RED)); getContentPane().add(toBeBordered) ... public ArrayList pages() return pages; // factory Method. abstract public void createPages ... – PowerPoint PPT presentation

Number of Views:24
Avg rating:3.0/5.0
Slides: 13
Provided by: rickm1
Category:

less

Transcript and Presenter's Notes

Title: Factory Method


1
Factory Method Design Pattern
CSC 335 Object-Oriented Programming and Design
2
OO Design PatternFactory Method
  • Name Factory
  • Problem A Client needs an object and it doesn't
    know which of several objects to instantiate
  • Solution Let an object instantiate the correct
    object from several choices. The return type is
    an abstract class or an interface type.

3
Characteristics
  • A method returns an object
  • The return type is an abstract class or interface
  • The interface is extended by or implemented by
    two or more classes
  • Is toString a factory method or just a factory?

4
BorderFactory?
  • Java has a BorderFactory class to return borders
    Ever JComponent can have a border
  • toBeBordered new JPanel()
  • toBeBordered.setBorder
  • (BorderFactory.createMatteBorder(2,1,5,9,Color
    .RED))
  • getContentPane().add(toBeBordered)
  • Is createMatteBorder a factory method or just a
    factory?
  • How does the intent of Factory Method differ from
    BorderFactory

5
Iterators?
  • The iterator methods isolate the client from
    knowing the class to instantiate
  • List list Arrays.asList(new String
    "a","b","c")
  • Iterator itr list.iterator()
  • while(itr.hasNext())
  • System.out.println(itr.next() " ") // a b
    c
  • What type is itr?
  • Is iterator a Factory method or just a factory?

6
Oozinoz Example
  • Allow customers to buy fireworks with credit
  • The credit agency may be online or offline
  • If online, let the credit company make the
    decision
  • If not online, send request as a dialog to the
    call center
  • Currently has these two classes

CreditCheckOnLine creditLimit(idString)double
CreditCheckOffLine creditLimit(idString)double
7
Use Factory Method
  • Make a design that allows the client to get an
    answer from either online or offline without
    worrying about which one
  • Draw a UML diagram for
  • interface CreditCheck with the creditLimit method
  • Two classes that implement it
  • A CreditCheckFactory that returns an instance of
    a class that implements CreditCheck with a method
    named createCreditCheck

8
Write the method
  • Assume CreditCheckFactory has method
    isAgencyOnLine to return true if the credit
    agency is available.
  • Write the complete method createCreditCheck

9
Product  (Page) defines the interface of objects
the factory method creates ConcreteProduct  (Ski
llsPage, EducationPage, ExperiencePage)
implements the Product interface Creator  (Documen
t) Declares the factory method, which returns an
object of type Product. ConcreteCreator  (Report,
Resume) overrides the factory method to return
an instance of a ConcreteProduct.
10
From DoFactory
11
Calls subclass's constructor
  • abstract class Document class Resume
    extends Document
  • protected ArrayList pages public void
    createPages()
  • new ArrayList()
  • pages.add(new
    EducationPage())
  • public Document() pages.add(new
    SkillsPage())
  • pages.add(new
    ExperiencePage())
  • this.createPages()
  • public ArrayList pages()
  • return pages
  • // factory Method
  • abstract public void createPages()

12
  • // Note Document's constructor calls the
    correct Factory
  • docs0 new Resume()
  • docs1 new Report()
  • for (int j 0 j lt docs.length j)
  • // Display document pages
  • Document document docsj
  • System.out.println("\n" document " -------
    ")
  • ArrayList pages document.pages()
  • Iterator itr pages.iterator()
  • while (itr.hasNext())
  • System.out.println(itr.next())
  • Output
  • factory.Resume_at_765291 -------
  • factory.SkillsPage_at_14f8dab
  • factory.EducationPage_at_1ddebc3
  • factory.ExperiencePage_at_a18aa2
  • factory.Report_at_194ca6c -------
Write a Comment
User Comments (0)
About PowerShow.com