Abstract Factory

Definition: Provides an interface for creating families of related or dependent objects without specifying their concrete classes.

Usage: Use an abstract factory when you need a factory method but need to create a whole family of objects, rather than just one.

Example

interface WidgetFactory {
	createButton()
	createLabel()
	createList()
	createTable()
}

class RoundWidgetFactory {
	createButton()
	createLabel()
	createList()
	createTable()
}

class SquareWidgetFactory {
	createButton()
	createLabel()
	createList()
	createTable()
}

This example makes sense only when all the widgets have something in common. For instance, each widget could foreground and background colors, shape, and font. This could be implemented with Strategy patterns (or simple variables in some cases) for each category. However, to set those properties for a whole bunch of widgets that need to be created, you would need to use a factory. If these properties were the same for all widgets, you would need only have some kind of global repository for this data.

Index

Copyright (C) 2008 Steven Fletcher. All rights reserved.