next up previous contents
Next: Object Pool Pattern: Reusable Up: Patterns Previous: Dynamic Starter Pattern: Runtime   Contents

Abstract Factory Pattern: Platform Independent GUI [56]



$\bullet$ Problem: How to port application software to new platforms without modifications?

$\bullet$ Solution: Abstract Factory Pattern

$\bullet$ Outline: Each platform requires specific code to achieve implementation. Hide the construction of the specific objects in a Factory per platform. The application calls the Factory to do the construction rather than explicitly calling ``new''.

Awkward solution is to keep a specialized source per platform:

class GUI_SUN {
}
class GUI_MS {
}
class GUI_APPLE {
}

Another awkward solution is compile-time C language ``defines'' to do conditional inclusion:

#define SUN 1
#define MS 2
#define APPLE 3
#define PLATFORM SUN

#if PLATFORM == SUN
  // put SUN code here
#elseif PLATFORM == MS
  // put MS code here
#elseif PLATFORM == APPLE
  // put APPLE code here

Abstract Factory solution:

$\bullet$ Code number (from environment file) determines platform

$\bullet$ Application (e.g. GUI) calls AbstractFactory.getFactory(code) to get a Factory

$\bullet$ Specialized sub-class Factory, implements the construction of specialized GUI objects

$\bullet$ Instead of calling ``new'', application asks Factory to construct object

$\bullet$ Application deals with Menu and Button, not SunMenu, MSMenu, SunButton, MSButton

$\bullet$ Application deals with AbstractFactory, not Sun Factory, MSFactory



Abstract Factory Pattern: Platform Independent GUI [57]




\begin{picture}(585,620)(40,190)\thicklines
\put(395,750){\framebox (135,50){...
...x(0,0)[lb]{\raisebox{0pt}[0pt][0pt]{\twlrm 5.1 println(''SMS'')}}}
\end{picture}

$\bullet$ Buttons and Menus are just stubs: push() just says ``Sun Button Push''

$\bullet$ f:SunFactory because the code is #1; but in GUI it is f:AbstractFactory


next up previous contents
Next: Object Pool Pattern: Reusable Up: Patterns Previous: Dynamic Starter Pattern: Runtime   Contents
Ted Billard 2006-09-26