17
The Flyweight Pattern
Sometimes in programming, it seems that you need to generate a very large number of small class instances to represent data. You can greatly reduce the number of different classes that you need to instantiate if you can recognize that the instances are fundamentally the same except for a few parameters. If you can move those variables outside the class instance and pass them in as part of a method call, the number of separate instances can be greatly reduced by sharing them.
The Flyweight design pattern provides an approach for handling such classes. It refers to the instance’s intrinsic data, which makes the instance unique, and the extrinsic data, which is passed in as arguments. The Flyweight pattern is appropriate ...
Get Python Programming with Design Patterns now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.