Type Object Pattern
A software design pattern that represents different variants of a concept using objects rather than subclasses. The core idea is to create a 'type class' that holds configuration data, while the main class holds a reference to a type instance. This approach avoids the proliferation of inheritance hierarchies; adding a new variant requires only instantiating a new object.
Timeline (last 90 days)
The core idea of the Type Object pattern is to represent variation using objects rather than the programming language's type system, letting the main class hold a reference to a type instance
The Type Object pattern was first systematically articulated by Ralph Johnson and Bobby Woolf in 《Pattern Languages of Program Design 3》
Inheritance expresses an is-a relationship, an enum expresses 'has a plan-type enum value', and a Type Object expresses a has-a relationship
The difference between the Type Object pattern and the Strategy pattern is: the Strategy pattern encapsulates interchangeable algorithms, while the Type Object encapsulates interchangeable configuration data, and the two can be used together
If the variation carries data and rules (such as price, limits, and feature sets), the variant should be considered as an object
Type Objects can be used in game development, for example using a MonsterType object to describe goblin attributes, with all goblin instances referencing the same type object
The combination of the Type Object pattern and protocols embodies the 'composition over inheritance' principle: using has-a relationships to manage configuration variants, and structural polymorphism to manage behavioral variants
If plan configurations are fixed and only change at deployment time, immutable objects can be defined in a separate plans.py file; if changes need to be made without deploying new code, they can be loaded from a database
All Facts (8)
The core idea of the Type Object pattern is to represent variation using objects rather than the programming language's type system, letting the main class hold a reference to a type instance
50%UnverifiedThe Type Object pattern was first systematically articulated by Ralph Johnson and Bobby Woolf in 《Pattern Languages of Program Design 3》
50%UnverifiedInheritance expresses an is-a relationship, an enum expresses 'has a plan-type enum value', and a Type Object expresses a has-a relationship
50%UnverifiedThe difference between the Type Object pattern and the Strategy pattern is: the Strategy pattern encapsulates interchangeable algorithms, while the Type Object encapsulates interchangeable configuration data, and the two can be used together
50%UnverifiedIf the variation carries data and rules (such as price, limits, and feature sets), the variant should be considered as an object
50%UnverifiedType Objects can be used in game development, for example using a MonsterType object to describe goblin attributes, with all goblin instances referencing the same type object
50%UnverifiedThe combination of the Type Object pattern and protocols embodies the 'composition over inheritance' principle: using has-a relationships to manage configuration variants, and structural polymorphism to manage behavioral variants
50%UnverifiedIf plan configurations are fixed and only change at deployment time, immutable objects can be defined in a separate plans.py file; if changes need to be made without deploying new code, they can be loaded from a database
50%