Skip to content

Commit

Permalink
Update lecture8.md
Browse files Browse the repository at this point in the history
  • Loading branch information
babis-k authored Oct 9, 2024
1 parent a9689d6 commit e20ea58
Showing 1 changed file with 42 additions and 30 deletions.
72 changes: 42 additions & 30 deletions lectures/lecture8.md
Original file line number Diff line number Diff line change
Expand Up @@ -501,43 +501,43 @@ void EvadeState::handle(Context& context) {
# Strategy Pattern
```CS
interface Integrator {
void step(float h);
}
```cpp
class Integrator {
public:
virtual void step(float h) = 0;
};
class LeapFrog : Integrator {
void step(float h) {
// Calculate forces
}
}
public:
void step(float h) override {} // use leapfrog verlet
};
class Euler : Integrator {
void step(float h) {
// Calculate forces
}
}
public:
void step(float h) override {} // use explicit euler
};
class Simulator {
Integrator integrationMethod;
public:
void update(float h) {
integrationMethod.step(h);
integrationMethod->step(h);
}
}
private:
Integrator * integrationMethod;
};
```
---
# Observer Pattern
- We want to have a centralised repository and control point for a collection of objects.
- The **subject** keeps track of all objects, the **observers**, and performs operations on them.
- The **subject** keeps track of all objects, the **observers**, and notifies them of any state changes.
- The subjects are registered at runtime.
- Example: An entity manger that keeps track of all entities in a game.
- Example: An entity manager that keeps track of all entities in a game.
- Entity manager is the subject.
- The entities are the observers.
- The entity manger calls methods like `update()` and `render()` each frame.
- The entity manager calls methods like `update()` and `render()` each frame
---
Expand All @@ -551,24 +551,36 @@ class Simulator {
# Observer Pattern
```CS
```cpp
class Entity { // Observer
public:
void update(float dt) {}
void render() {}
};
class EntityManager { // Subject
List<Entity> entities = new List<Entity>();
std::vector<Entity*> entities;
void update(float dt) {
for (Entity entity in entities) {
entity.update(dt);
for (Entity* entity : entities) {
entity->update(dt);
}
}
}
// register(), unregister(), etc.
}
void render() {
for (Entity* entity : entities) {
entity->render();
}
}
class Entity { // Observer
void update(float dt) {
// ...
void registerEntity(Entity * entity) {
entities.push_back(entity);
}
}
void unregisterEntity(Entity* entity) {
// ... remove entity from entities vector
}
};
```
---
Expand Down

0 comments on commit e20ea58

Please sign in to comment.