Book Description

About the Author

Table of Contents

Source Code

Errata

Send Comments

Teaching Materials

Author's Homepage
 

Object-Oriented Software Development Using Java
Xiaoping Jia, Ph.D
Chapter 10: More Design Patterns

Contents
   10.1 Type-Safe Enumeration Type
         10.1.1 A Simple Maze Game
         10.1.2 Enumeration Types
         10.1.3 Unordered Type-Safe Enumeration Idiom
         10.1.4 Ordered Type-Safe Enumeration Idiom
   10.2 Creational Design Patterns
         10.2.1 A Simple Design of the Maze Game
         10.2.2 Design Pattern: Abstract Factory
         10.2.3 Design Pattern: Factory Method
         10.2.4 Design Pattern: Prototype
         10.2.5 Design Pattern: Builder
   10.3 Behavioral Patterns
         10.3.1 Design Pattern: Command
         10.3.2 Supporting Undo
   10.4 Structural Patterns
         10.4.1 Design Pattern: Adapter
         10.4.2 Design Pattern: Composite



Other Chapters

Type-Safe Enumeration (Implementation Pattern)
  • Direction.java - an enumeration type of directions: NORHT, EAST, SOUTH, and WEST
  • Orientation.java - an enumeration type of orientations: HORIZONTAL and VERTICAL


Abstract Factory (Creational Pattern)
  • The Maze game
    • Design
MapSite class diagram
    • Implementation
      • MapSite.java - an interface for map sites in a maze game
        • Room.java - a room in a maze game, implements MapSite
        • Door.java - a door in a maze game, implements MapSite
        • Wall.java - a wall in a maze game, implements MapSite
      • Maze.java  - a maze game
      • SimpleMazeGame.java - a simple implementation of the maze game that does not use creational patterns
        • Run the program as follows:

        • java maze.SimpleMazeGame
                     - create a small two-room Maze game [screen shot]
          java maze.SimpleMazeGame Large
                     - create a large nine-room Maze game [screen shot]
        • Use the arrow keys to move the palyer.
  • A design and implementation of the Maze game using Abstract Factory pattern
    • Desgin

    • Implementation
      • Abstract factories
        • MazeFactory.java - a factory that creates default style rooms, doors, and walls for the Maze game
        • HarryPotterMazeFactory.java  -  a factory that creates "Harry Potter" style rooms, doors, and walls for the Maze game, extends MazeFactory
        • SnowWhiteMazeFactory.java  -  a factory that creates "Snow White" style rooms, doors, and walls for the Maze game, extends MazeFactory
      • Concrete products
      • MazeGameAbstractFactory.java -  an implementation of the maze game that uses the Abstract Factory pattern
        • Run the program as follows:

        • java maze.MazeGameAbstractFactory Harry
                     - create a "Harry Potter" style Maze game  using the HarryPotterMazeFactory [screen shot]
          java maze.MazeGameAbstractFactory Snow
                     - create a "Snow White" style Maze game  using the SnowWhiteMazeFactory [screen shot]
          java maze.MazeGameAbstractFactory
                     - create a default  style Maze game  using the default MazeFactory [screen shot]


Factory Method (Creational Pattern)
  • Design

  • Implementation


Prototype (Creational Pattern)
  • Design

  • Implementation
    • MazePrototypeFactory.java  - An abstract factory for building maze game using Prototype design pattern
    • Run the program as follows:

    • java maze.MazePrototypeFactory Harry
         - create a "Harry Potter" style Maze game  using instances of HarryPotterDoor, HarryPotterRoom, and HarryPotterWall as prototypes [screen shot]
      java maze.MazePrototypeFactory Snow
         - create a "Snow White" style Maze game  using instances of SnowWhiteDoor, SnowWhiteRoom, and SnowWhiteWall as prototypes [screen shot]
      java maze.MazePrototypeFactory
         - create a default  style Maze game  using instances of Door, Room, and Wall as prototypes [screen shot]
  • A universal factory that uses Abstract Factory, Prototype and Singleton design patterns.
    • UniversalMazeFactory.java
    • It uses the following system properties to determine which factory will be used.
      • maze.prototype
        • "true "or "false": whether to use prototype based factory or not
      • maze.theme
        • "Harry", "Snow", or "Default": the theme used
    • Run the program as follows:

    • java -Dmaze.prototype=proto -Dmaze.theme=theme maze.MazeUniversalFactory
      Note: the -D options must come before the class name.


Builder (Creational Pattern)
  • Design

  • Implementation
    • The maze builders
    • The driver class
      • MazeGameBuilder.java
      • Run the program as follows:

      • java maze.MazeGameBuilder Harry
          - create a "Harry Potter" style Maze game  using the FactoryMazeBuilder with HarryPotterMazeFactory [screen shot]
        java maze.MazeGameBuilder Snow
          - create a "Snow White" style Maze game  using the FactoryMazeBuilder with SnowWhiteMazeFactory [screen shot]
        java maze.MazeGameBuilder Default
          - create a default style Maze game  using the FactoryMazeBuilder with the default MazeFactory [screen shot]
        java maze.MazeGameBuilder
          - create a default  style Maze game  using the SimpleMazeBuilder [screen shot]


Command (Behavioral Pattern)
  • Design

  • Implementation
    • The commands
    • The driver class
      • UndoableMazeGame.java
      • Run the program as follows:

      • java maze.UndoableMazeGame Harry
          - create a "Harry Potter" style undoable maze game  using the FactoryMazeBuilder with HarryPotterMazeFactory [screen shot]
        java maze.UndoableMazeGame Snow
          - create a "Snow White" style undoable maze game  using the FactoryMazeBuilder with SnowWhiteMazeFactory [screen shot]
        java maze.UndoableMazeGame Default
          - create a default style undoable maze game  using the FactoryMazeBuilder with the default MazeFactory [screen shot]
        java maze.UndoableMazeGame
          - create a default style undoable maze game  using the SimpleMazeBuilder [screen shot]


Adapter (Structural Pattern)
  • Design


  •  

    Adapter using inheritance

    Adapter using deligation


     

  • Implementation
    • Table.java - a generic table
    • TableEntry.java - an interface for the entries to be displayed in the table, target of the adapter pattern
    • Student.java - the adaptee class
    • StudentEntry.java - an adapter using inheritance
    • StudentEntry2.java - an adapter using deligation
    • Main.java - the driver class
      • Run the program as follows: [screen shot]

      • java adapter.Main
          - show a student table using an inheritance-based adapter, StudentEntry
        java adapter.Main Deligation
          - show a student table using a deligation-based adapter, StudentEntry2


Composite (Structural Pattern)

Book Description - About the Author - Table of Contents
Source Code - Errata - Send Comments
Teaching Materials - Author's Homepage