Skip to content

Commit 48a5ede

Browse files
committed
docs: update Component documentation
1 parent 81ebcf0 commit 48a5ede

File tree

1 file changed

+101
-56
lines changed

1 file changed

+101
-56
lines changed

component/README.md

Lines changed: 101 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,36 @@
11
---
22
title: Component
3-
categories: Behavioral
3+
categories: Structural
44
language: en
55
tag:
6-
- Game programming
7-
- Domain
6+
- Game programming
7+
- Decoupling
8+
- Modularity
89
---
910

11+
## Also known as
12+
13+
* Entity-Component-System (ECS)
14+
* Component-Entity-System (CES)
15+
* Component-Based Architecture (CBA)
16+
1017
## Intent
1118

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.
1621

1722
![Intent](./etc/component.duplication.png "Component Design Pattern")
1823

1924
## Explanation
2025

2126
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.
2334
2435

2536
In plain words
@@ -63,48 +74,48 @@ components.
6374

6475
```java
6576
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+
}
108119
}
109120
```
110121

@@ -148,13 +159,47 @@ public class PlayerInputComponent implements InputComponent {
148159

149160
## Applicability
150161

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
152191

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.
156198

157199
## Credits
158200

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

Comments
 (0)