University of Cincinnati logo and link  
CinJUG: Design Patterns, October 16, 2006
 
 

Boycott if-tests and conditionals: use design patterns for optimal design and flexibility

UC ingot

Agenda:

1) Introductions
2) What are design patterns?  Why use them?
3) Description of Factory and Command patterns.

4) Live example.

Quotes:
"If you're not using interfaces, you're only using half of Java's potential."
"You can judge how well an object-oriented program is written by how few if-tests it uses."


Problem Statement:
  • The challenge: write a program that processes  files.
    • The files are deposited into one directory.
    • The files can contain information about one of the following:
      • Employee payroll
      • Life Insurance bills
      • Annuity payments.
    • There may be 0 to many files of these types, in any order, in the directory.
    • Each file contains information that the program will use to process the file (pay employees, send life insurance invoices, send annuity payments).
    • The program must be flexible.  It must be able to add and remove new lines of business at any time.
    • If a new file type occurs, say, Business, we need to be able to read this file from File Reader.  But, we cannot make any changes to FileReader, or any of the other classes we've already written.
    • Further, if tests are strictly not allowed.
  • What will we need to do this?
    • Interfaces
    • Factory Pattern
    • Command Pattern
    • Class Class, methods forName() and newInstance()
    • Class java.util.Scanner.
    • Class java.io.File.
  • Resources used in this presentation:
    • Design Patterns, Elements of Reusable Object-Oriented Software, Gamma, Helm, Johnson, Vlissides, 1995, Addison-Wesley.
      • Quoted as 'GoF' in this presentation.
    • www.patterndepot.com
Patterns

 Cincinnati Java Users Group