Python Design Patterns Tutorial
Python Design Patterns Resources
Selected Reading
- Exception Handling
- Python Design Patterns - Anti
- Concurrency in Python
- Strings & Serialization
- Python Design Patterns - Queues
- Python Design Patterns - Sets
- Lists Data Structure
- Dictionaries
- Python Design Patterns - Iterator
- Object Oriented Concepts Implementation
- Object Oriented
- Abstract Factory
- Python Design Patterns - Flyweight
- Python Design Patterns - Template
- Python Design Patterns - Strategy
- Python Design Patterns - State
- Python Design Patterns - Observer
- Chain of Responsibility Pattern
- Python Design Patterns - Proxy
- Python Design Patterns - Decorator
- Python Design Patterns - Adapter
- Python Design Patterns - Command
- Python Design Patterns - Facade
- Python Design Patterns - Prototype
- Python Design Patterns - Builder
- Python Design Patterns - Factory
- Python Design Patterns - Singleton
- Model View Controller Pattern
- Python Design Patterns - Gist
- Introduction
- Python Design Patterns - Home
Python Design Patterns Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Abstract Factory
Python Design Patterns - Abstract Factory
The abstract factory pattern is also called factory of factories. This design pattern comes under the creational design pattern category. It provides one of the best ways to create an object.
It includes an interface, which is responsible for creating objects related to Factory.
How to implement the abstract factory pattern?
The following program helps in implementing the abstract factory pattern.
class Window: __toolkit = "" __purpose = "" def __init__(self, toolkit, purpose): self.__toolkit = toolkit self.__purpose = purpose def getToolkit(self): return self.__toolkit def getType(self): return self.__purpose class GtkToolboxWindow(Window): def __init__(self): Window.__init__(self, "Gtk", "ToolboxWindow") class GtkLayersWindow(Window): def __init__(self): Window.__init__(self, "Gtk", "LayersWindow") class GtkMainWindow(Window): def __init__(self): Window.__init__(self, "Gtk", "MainWindow") class QtToolboxWindow(Window): def __init__(self): Window.__init__(self, "Qt", "ToolboxWindow") class QtLayersWindow(Window): def __init__(self): Window.__init__(self, "Qt", "LayersWindow") class QtMainWindow(Window): def __init__(self): Window.__init__(self, "Qt", "MainWindow") # Abstract factory class class UIFactory: def getToolboxWindow(self): pass def getLayersWindow(self): pass def getMainWindow(self): pass class GtkUIFactory(UIFactory): def getToolboxWindow(self): return GtkToolboxWindow() def getLayersWindow(self): return GtkLayersWindow() def getMainWindow(self): return GtkMainWindow() class QtUIFactory(UIFactory): def getToolboxWindow(self): return QtToolboxWindow() def getLayersWindow(self): return QtLayersWindow() def getMainWindow(self): return QtMainWindow() if __name__ == "__main__": gnome = True kde = not gnome if gnome: ui = GtkUIFactory() epf kde: ui = QtUIFactory() toolbox = ui.getToolboxWindow() layers = ui.getLayersWindow() main = ui.getMainWindow() print "%s:%s" % (toolbox.getToolkit(), toolbox.getType()) print "%s:%s" % (layers.getToolkit(), layers.getType()) print "%s:%s" % (main.getToolkit(), main.getType())
Output
The above program generates the following output −
Explanation
In the above program, the abstract factory creates objects for each window. It calls for each method, which executes the output as expected.
Advertisements