Problem: How to port application software to new platforms without modifications?
Solution: Abstract Factory Pattern
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:
Code number (from environment file) determines platform
Application (e.g. GUI) calls AbstractFactory.getFactory(code) to get a Factory
Specialized sub-class Factory, implements the construction of specialized GUI objects
Instead of calling ``new'', application asks Factory to construct object
Application deals with Menu and Button, not SunMenu, MSMenu, SunButton, MSButton
Application deals with AbstractFactory, not Sun Factory, MSFactory
Abstract Factory Pattern: Platform Independent GUI [57]
Buttons and Menus are just stubs: push() just says ``Sun Button Push''
f:SunFactory because the code is #1; but in GUI it is f:AbstractFactory