|
1 | 1 | --- |
2 | 2 | title: Component |
3 | | -categories: Behavioral |
| 3 | +categories: Structural |
4 | 4 | language: en |
5 | 5 | tag: |
6 | | -- Game programming |
7 | | -- Domain |
| 6 | + - Game programming |
| 7 | + - Decoupling |
| 8 | + - Modularity |
8 | 9 | --- |
9 | 10 |
|
| 11 | +## Also known as |
| 12 | + |
| 13 | +* Entity-Component-System (ECS) |
| 14 | +* Component-Entity-System (CES) |
| 15 | +* Component-Based Architecture (CBA) |
| 16 | + |
10 | 17 | ## Intent |
11 | 18 |
|
12 | | -The component design pattern enables developers to decouple attributes of an objects. Essentially allowing a single |
13 | | -component to be inheritable by multiple domains/objects without linking the objects to each other. In addition to this |
14 | | -benefit, the component design pattern allows developer to write maintainable and comprehensible code which is less |
15 | | -likely to result in monolithic classes. |
| 19 | +The Component design pattern aims to organize code into reusable, interchangeable components, promoting flexibility and |
| 20 | +ease of maintenance in game development by allowing entities to be configured with varying behaviors. |
16 | 21 |
|
17 | 22 |  |
18 | 23 |
|
19 | 24 | ## Explanation |
20 | 25 |
|
21 | 26 | Real world example |
22 | | -> Suppose your video game consists of a graphics component and a sound component. Including the methods and attributes of both of these features in a single java class can be problematic due to many reasons. Firstly, the graphics and sound code can create an extremely long java class which can be hard to maintain. Furthermore, graphics components may be written and implemented by a separate team as to the sound contents. If both parties work simultaneously on the same java class, this may cause conflicts and major delay. Using the component design pattern, the development team is able to create individual component classes for graphics and sound whilst providing the domain/object the reach to both of these attributes. |
| 27 | +> Suppose your video game consists of a graphics component and a sound component. Including the methods and attributes |
| 28 | +> of both of these features in a single java class can be problematic due to many reasons. Firstly, the graphics and sound |
| 29 | +> code can create an extremely long java class which can be hard to maintain. Furthermore, graphics components may be |
| 30 | +> written and implemented by a separate team as to the sound contents. If both parties work simultaneously on the same |
| 31 | +> java class, this may cause conflicts and major delay. Using the component design pattern, the development team is able |
| 32 | +> to create individual component classes for graphics and sound whilst providing the domain/object the reach to both of |
| 33 | +> these attributes. |
23 | 34 |
|
24 | 35 |
|
25 | 36 | In plain words |
@@ -63,48 +74,48 @@ components. |
63 | 74 |
|
64 | 75 | ```java |
65 | 76 | public class GameObject { |
66 | | - private final InputComponent inputComponent; |
67 | | - private final PhysicComponent physicComponent; |
68 | | - private final GraphicComponent graphicComponent; |
69 | | - |
70 | | - public String name; |
71 | | - public int velocity = 0; |
72 | | - public int coordinate = 0; |
73 | | - |
74 | | - public static GameObject createPlayer() { |
75 | | - return new GameObject(new PlayerInputComponent(), |
76 | | - new ObjectPhysicComponent(), |
77 | | - new ObjectGraphicComponent(), |
78 | | - "player"); |
79 | | - } |
80 | | - |
81 | | - public static GameObject createNpc() { |
82 | | - return new GameObject( |
83 | | - new DemoInputComponent(), |
84 | | - new ObjectPhysicComponent(), |
85 | | - new ObjectGraphicComponent(), |
86 | | - "npc"); |
87 | | - } |
88 | | - |
89 | | - public void demoUpdate() { |
90 | | - inputComponent.update(this); |
91 | | - physicComponent.update(this); |
92 | | - graphicComponent.update(this); |
93 | | - } |
94 | | - |
95 | | - public void update(int e) { |
96 | | - inputComponent.update(this, e); |
97 | | - physicComponent.update(this); |
98 | | - graphicComponent.update(this); |
99 | | - } |
100 | | - |
101 | | - public void updateVelocity(int acceleration) { |
102 | | - this.velocity += acceleration; |
103 | | - } |
104 | | - |
105 | | - public void updateCoordinate() { |
106 | | - this.coordinate += this.velocity; |
107 | | - } |
| 77 | + private final InputComponent inputComponent; |
| 78 | + private final PhysicComponent physicComponent; |
| 79 | + private final GraphicComponent graphicComponent; |
| 80 | + |
| 81 | + public String name; |
| 82 | + public int velocity = 0; |
| 83 | + public int coordinate = 0; |
| 84 | + |
| 85 | + public static GameObject createPlayer() { |
| 86 | + return new GameObject(new PlayerInputComponent(), |
| 87 | + new ObjectPhysicComponent(), |
| 88 | + new ObjectGraphicComponent(), |
| 89 | + "player"); |
| 90 | + } |
| 91 | + |
| 92 | + public static GameObject createNpc() { |
| 93 | + return new GameObject( |
| 94 | + new DemoInputComponent(), |
| 95 | + new ObjectPhysicComponent(), |
| 96 | + new ObjectGraphicComponent(), |
| 97 | + "npc"); |
| 98 | + } |
| 99 | + |
| 100 | + public void demoUpdate() { |
| 101 | + inputComponent.update(this); |
| 102 | + physicComponent.update(this); |
| 103 | + graphicComponent.update(this); |
| 104 | + } |
| 105 | + |
| 106 | + public void update(int e) { |
| 107 | + inputComponent.update(this, e); |
| 108 | + physicComponent.update(this); |
| 109 | + graphicComponent.update(this); |
| 110 | + } |
| 111 | + |
| 112 | + public void updateVelocity(int acceleration) { |
| 113 | + this.velocity += acceleration; |
| 114 | + } |
| 115 | + |
| 116 | + public void updateCoordinate() { |
| 117 | + this.coordinate += this.velocity; |
| 118 | + } |
108 | 119 | } |
109 | 120 | ``` |
110 | 121 |
|
@@ -148,13 +159,47 @@ public class PlayerInputComponent implements InputComponent { |
148 | 159 |
|
149 | 160 | ## Applicability |
150 | 161 |
|
151 | | -Use the component design pattern when |
| 162 | +* Used in game development and simulations where game entities (e.g., characters, items) can have a dynamic set of |
| 163 | + abilities or states. |
| 164 | +* Suitable for systems requiring high modularity and systems where entities might need to change behavior at runtime |
| 165 | + without inheritance hierarchies. |
| 166 | + |
| 167 | +## Known Uses |
| 168 | + |
| 169 | +* Game engines like Unity, Unreal Engine, and various custom engines in AAA and indie games. |
| 170 | +* Simulation systems that require flexible, dynamic object composition. |
| 171 | + |
| 172 | +## Consequences |
| 173 | + |
| 174 | +Benefits: |
| 175 | + |
| 176 | +* Flexibility and Reusability: Components can be reused across different entities, making it easier to add new features |
| 177 | + or modify existing ones. |
| 178 | +* Decoupling: Reduces dependencies between game entity states and behaviors, facilitating easier changes and |
| 179 | + maintenance. |
| 180 | +* Dynamic Composition: Entities can alter their behavior at runtime by adding or removing components, providing |
| 181 | + significant flexibility in game design. |
| 182 | + |
| 183 | +Trade-offs: |
| 184 | + |
| 185 | +* Complexity: Can introduce additional complexity in system architecture, particularly in managing dependencies and |
| 186 | + communications between components. |
| 187 | +* Performance Considerations: Depending on implementation, may incur a performance overhead due to indirection and |
| 188 | + dynamic behavior, especially critical in high-performance game loops. |
| 189 | + |
| 190 | +## Related Patterns |
152 | 191 |
|
153 | | -- you have a class which access multiple features which you would like to keep separate. |
154 | | -- you want to reduce the length of a class. |
155 | | -- you require a variety of objects to share a collection of components but the use of inheritance isn't specific enough. |
| 192 | +* [Decorator](https://java-design-patterns.com/patterns/decorator/): Similar concept of adding responsibilities |
| 193 | + dynamically, but without the focus on game entities. |
| 194 | +* [Flyweight](https://java-design-patterns.com/patterns/flyweight/): Can be used in conjunction with the Component |
| 195 | + pattern to share component instances among many entities to save memory. |
| 196 | +* [Observer](https://java-design-patterns.com/patterns/observer/): Often used in Component systems to communicate state |
| 197 | + changes between components. |
156 | 198 |
|
157 | 199 | ## Credits |
158 | 200 |
|
159 | | -- [Component Design Pattern] (https://gameprogrammingpatterns.com/component.html) |
160 | | -- [Component pattern - game programming series - Tutemic] (https://www.youtube.com/watch?v=n92GBp2WMkg&ab_channel=Tutemic) |
| 201 | +* [Component Design Pattern] (https://gameprogrammingpatterns.com/component.html) |
| 202 | +* [Component pattern - game programming series - Tutemic] (https://www.youtube.com/watch?v=n92GBp2WMkg&ab_channel=Tutemic) |
| 203 | +* [Game Programming Patterns](https://amzn.to/4cDRWhV) |
| 204 | +* [Unity in Action: Multiplatform Game Development in C#](https://amzn.to/3THO6vw) |
| 205 | +* [Procedural Content Generation for Unity Game Development](https://amzn.to/3vBKCTp) |
0 commit comments