How are GoF patterns used in Java and Python?
Answer
In Java, GoF patterns are deeply embedded in the standard library and ecosystem. Singleton is used for Runtime.getRuntime(); Factory Method in Calendar.getInstance(); Abstract Factory in the DocumentBuilderFactory (XML API); Iterator in java.util.Iterator; Observer in java.util.EventListener; Decorator in java.io streams; Flyweight in Integer.valueOf() caching. In Python, the language's dynamic nature means many patterns are simpler or built-in: Iterator via __iter__/__next__, Observer via signals (Django) or callbacks, Decorator via Python's native @decorator syntax (though not exactly GoF Decorator), Singleton via module-level instances. Python's duck typing often replaces the need for explicit interface definitions that Java requires for patterns.
Previous
What is the difference between Chain of Responsibility and Decorator?
Next
What is the Interpreter design pattern?
More Design Patterns (Gang of Four) Questions
View all →- Intermediate What is the Template Method design pattern?
- Intermediate What is the Visitor design pattern?
- Intermediate What is the difference between Factory Method and Abstract Factory?
- Intermediate When should you NOT use design patterns?
- Intermediate How are the Gang of Four patterns categorized?