Skip to content

Commit ffbcbdc

Browse files
docs: Update README.md of typeObjectpattern (#2596)
Fixes #2266 Explanation for Type-Object
1 parent 1bea90e commit ffbcbdc

File tree

1 file changed

+54
-5
lines changed

1 file changed

+54
-5
lines changed

typeobjectpattern/README.md

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,74 @@ tag:
77
- Extensibility
88
---
99

10+
# Type-Object Pattern in Java
11+
12+
## Explanation
13+
14+
In Java, the Type-Object pattern is a design pattern that encapsulates type information in an object. This pattern is particularly useful when dealing with multiple objects of the same kind, and there is a need to add new types without altering existing code.
15+
1016
## Intent
1117
As explained in the book Game Programming Patterns by Robert Nystrom, type object pattern helps in
1218

1319
> Allowing flexible creation of new “classes” by creating a single class, each instance of which represents a different type of object
1420
15-
## Explanation
16-
Say, we are working on a game which has a hero and many monsters which are going to attack the hero. These monsters have certain attributes like attack, points etc. and come in different 'breeds' like zombie or ogres. The obvious answer is to have a base Monster class which has some fields and methods, which may be overriden by subclasses like the Zombie or Ogre class. But as we continue to build the game, there may be more and more breeds of monsters added and certain attributes may need to be changed in the existing monsters too. The OOP solution of inheriting from the base class would not be an efficient method in this case.
21+
## Real World Example
22+
Let's consider a real-world example. Say, we are working on a game which has a hero and many monsters which are going to attack the hero. These monsters have certain attributes like attack, points etc. and come in different 'breeds' like zombie or ogres. The obvious answer is to have a base Monster class which has some fields and methods, which may be overriden by subclasses like the Zombie or Ogre class. But as we continue to build the game, there may be more and more breeds of monsters added and certain attributes may need to be changed in the existing monsters too. The OOP solution of inheriting from the base class would not be an efficient method in this case.
1723
Using the type-object pattern, instead of creating many classes inheriting from a base class, we have 1 class with a field which represents the 'type' of object. This makes the code cleaner and object instantiation also becomes as easy as parsing a json file with the object properties.
1824

19-
## Class diagram
20-
![alt text](./etc/typeobjectpattern.urm.png "Type-Object pattern class diagram")
25+
## In Plain Words
26+
27+
The Type-Object pattern in Java is a method to encapsulate type-specific properties and behaviors within an object. This design pattern facilitates the addition of new types without necessitating changes to existing code, thereby enhancing codebase expansion and maintenance.
28+
29+
## Wikipedia Says
30+
31+
While there isn't a specific Wikipedia entry for the Type-Object pattern, it is a commonly used technique in object-oriented programming. This pattern assists in managing objects that share similar characteristics but have different values for those characteristics. It finds widespread use in game development, where numerous types of objects (like enemies) share common behavior but have different properties.
32+
33+
## Programmatic Example
34+
35+
Consider an example involving different types of enemies in a game. Each enemy type has distinct properties like speed, health, and damage.
36+
37+
```java
38+
public class EnemyType {
39+
private String name;
40+
private int speed;
41+
private int health;
42+
private int damage;
43+
44+
public EnemyType(String name, int speed, int health, int damage) {
45+
this.name = name;
46+
this.speed = speed;
47+
this.health = health;
48+
this.damage = damage;
49+
}
50+
51+
// getters and setters
52+
}
53+
54+
public class Enemy {
55+
private EnemyType type;
56+
57+
// Encapsulating type information in an object
58+
public Enemy(EnemyType type) {
59+
this.type = type;
60+
}
61+
62+
// other methods
63+
}
64+
```
65+
66+
In the above example, `EnemyType` encapsulates type-specific properties (name, speed, health, damage), and `Enemy` uses an instance of `EnemyType` to define its type. This way, you can add as many enemy types as you want without modifying the `Enemy` class.
2167

2268
## Applicability
2369
This pattern can be used when:
2470

2571
* We don’t know what types we will need up front.
2672
* We want to be able to modify or add new types without having to recompile or change code.
2773
* Only difference between the different 'types' of objects is the data, not the behaviour.
28-
74+
75+
## Another example with class diagram
76+
![alt text](./etc/typeobjectpattern.urm.png "Type-Object pattern class diagram")
77+
2978
## Credits
3079

3180
* [Game Programming Patterns - Type Object](http://gameprogrammingpatterns.com/type-object.html)

0 commit comments

Comments
 (0)