Skip to content

Instantly share code, notes, and snippets.

The evolution demonstrates:
Starting with a single strategy for a game action.
Expanding to multiple strategies executed together.
Refactoring into a subject-observer structure.
Adding dynamic management for an arbitrary number of observers.
Strategy (Step 1): A single game-related behavior (e.g., starting the game) is encapsulated and executed, making it interchangeable with other behaviors like pausing or exiting.
Observer (Step 4): Multiple game components (observers) are notified of the button press and react in their own way (e.g., starting the game, playing a sound, updating the score).
@kenpower
kenpower / -Observer Pattern to improve spaghetti code.md
Last active March 12, 2025 22:00
Applying Observer pattern to game entities with multiple dependents

Summary of the Observer Pattern Implementation

The code demonstrates a game system refactored to use the Observer pattern, consisting of these key components:

  • Observer Interface (GameObserver): Defines methods for different game events (player damaged, player death, enemy death).

  • Subject Interface (Subject): Manages a list of observers and provides methods to add, remove, and notify observers.

  • Game Entities (Player and Enemy): Inherit from Subject and notify observers when their state changes.

@kenpower
kenpower / null.cpp
Created February 5, 2025 09:38
Null object Logger
#include <iostream>
#include <string>
// Logger Interface
class Logger {
public:
virtual void log(const std::string& message) = 0;
virtual ~Logger() {}
};
#ifdef _DEBUG
#pragma comment(lib,"sfml-graphics-d.lib")
#pragma comment(lib,"sfml-audio-d.lib")
#pragma comment(lib,"sfml-system-d.lib")
#pragma comment(lib,"sfml-window-d.lib")
#pragma comment(lib,"sfml-network-d.lib")
//#pragma comment(lib,"thor-d.lib")
#else
#pragma comment(lib,"sfml-graphics.lib")
#pragma comment(lib,"sfml-audio.lib")
@kenpower
kenpower / dip.cpp
Last active February 29, 2024 10:11
#ifdef _DEBUG
#pragma comment(lib,"sfml-graphics-d.lib")
#pragma comment(lib,"sfml-audio-d.lib")
#pragma comment(lib,"sfml-system-d.lib")
#pragma comment(lib,"sfml-window-d.lib")
#pragma comment(lib,"sfml-network-d.lib")
//#pragma comment(lib,"thor-d.lib")
#else
#pragma comment(lib,"sfml-graphics.lib")
#pragma comment(lib,"sfml-audio.lib")
#include <iostream>
#include <vector>
#include <string>
class Book {
public:
std::string title;
double price;
Book(std::string t, double p) : title(t), price(p) {}
};
@kenpower
kenpower / null.cpp
Last active February 14, 2024 10:26
Null Object
#include <iostream>
class ChaseStrategy{
public:
virtual void chase() = 0;
};
class RandomChase : public ChaseStrategy {
void chase() override {
std::cout << "Randomly wander around\n";
#include <iostream>
#include <string>
#include <vector>
class Drawable {
public:
virtual void draw() const = 0;
};
class Shape : public Drawable {
#include <iostream>
#include <memory>
#include <string>
// English implementation
class EnglishChatbot {
public:
void hello() override {
std::cout << "Hello!" << std::endl;
}
// Name:
// Login:
// Date:
// Approximate time taken:
//---------------------------------------------------------------------------
// Project description: TEMPLATE
// ---------------------------------------------------------------------------
// Known Bugs:
// ?