From ef549837844d0816231a50e891475f3c2b95b515 Mon Sep 17 00:00:00 2001 From: b0rbor4d Date: Wed, 15 Apr 2026 00:11:19 +0200 Subject: [PATCH] Add module8_modularization.c --- src/c_examples/module8_modularization.c | 268 ++++++++++++++++++++++++ 1 file changed, 268 insertions(+) create mode 100644 src/c_examples/module8_modularization.c diff --git a/src/c_examples/module8_modularization.c b/src/c_examples/module8_modularization.c new file mode 100644 index 0000000..a9c5e3a --- /dev/null +++ b/src/c_examples/module8_modularization.c @@ -0,0 +1,268 @@ +// ============================================ +// Modul 8: Modularisierung & Design Patterns +// Beispiele in C +// ============================================ + +// Dieses Beispiel demonstriert modularen Code-Organisation +// In echtem Projekt: Jede "Klasse" in eigener .c/.h Datei + +#include +#include +#include +#include + +// ============================================ +// BEISPIEL: Modulare Architektur (MVC-ähnlich) +// ============================================ + +// ============ MODEL (Daten) ============ +typedef struct { + float balance; + char owner[50]; +} Account; + +Account* account_create(const char* owner, float initial_balance) { + Account* a = malloc(sizeof(Account)); + strcpy(a->owner, owner); + a->balance = initial_balance; + return a; +} + +void account_destroy(Account* a) { + free(a); +} + +// ============ SERVICE (Business Logic) ============ +// Interface (abstrakt) +typedef struct { + int (*deposit)(Account* account, float amount); + int (*withdraw)(Account* account, float amount); + float (*get_balance)(Account* account); +} BankingService; + +// Implementation +int service_deposit(Account* a, float amount) { + if (amount <= 0) return -1; + a->balance += amount; + return 0; +} + +int service_withdraw(Account* a, float amount) { + if (amount <= 0 || amount > a->balance) return -1; + a->balance -= amount; + return 0; +} + +float service_get_balance(Account* a) { + return a->balance; +} + +BankingService* banking_service_create() { + BankingService* s = malloc(sizeof(BankingService)); + s->deposit = service_deposit; + s->withdraw = service_withdraw; + s->get_balance = service_get_balance; + return s; +} + +// ============ REPOSITORY (Data Access) ============ +// Abstraktion für Speicherung +typedef struct { + void (*save)(Account* account); + Account* (*load)(const char* owner); +} AccountRepository; + +// Mock Implementation (In-Memory) +static Account* mock_storage = NULL; + +void mock_save(Account* account) { + if (mock_storage) free(mock_storage); + mock_storage = account_create(account->owner, account->balance); + printf("[Repository] Account saved: %s (%.2f)\n", account->owner, account->balance); +} + +Account* mock_load(const char* owner) { + if (mock_storage && strcmp(mock_storage->owner, owner) == 0) { + printf("[Repository] Account loaded: %s\n", owner); + return account_create(mock_storage->owner, mock_storage->balance); + } + return NULL; +} + +AccountRepository* repository_create() { + AccountRepository* r = malloc(sizeof(AccountRepository)); + r->save = mock_save; + r->load = mock_load; + return r; +} + +// ============ CONTROLLER (Flow Control) ============ +typedef struct { + BankingService* service; + AccountRepository* repository; +} AccountController; + +void controller_create_account(AccountController* ctrl, const char* owner, float initial) { + Account* a = account_create(owner, initial); + ctrl->repository->save(a); + printf("[Controller] Created account for %s\n", owner); + account_destroy(a); +} + +void controller_deposit(AccountController* ctrl, const char* owner, float amount) { + Account* a = ctrl->repository->load(owner); + if (a) { + if (ctrl->service->deposit(a, amount) == 0) { + printf("[Controller] Deposited %.2f to %s\n", amount, owner); + ctrl->repository->save(a); + } else { + printf("[Controller] Deposit failed\n"); + } + account_destroy(a); + } +} + +void controller_show_balance(AccountController* ctrl, const char* owner) { + Account* a = ctrl->repository->load(owner); + if (a) { + float balance = ctrl->service->get_balance(a); + printf("[Controller] Balance for %s: %.2f\n", owner, balance); + account_destroy(a); + } +} + +// ============ VIEW (Presentation) ============ +void view_show_menu() { + printf("\n=== Banking System ===\n"); + printf("1. Create Account\n"); + printf("2. Deposit\n"); + printf("3. Show Balance\n"); + printf("4. Exit\n"); +} + +void view_show_error(const char* msg) { + printf("[ERROR] %s\n", msg); +} + +void view_show_success(const char* msg) { + printf("[SUCCESS] %s\n", msg); +} + +// ============================================ +// BEISPIEL: Strategy Pattern +// ============================================ + +// Interface für Sortierstrategie +typedef struct { + void (*sort)(int* arr, int size); + const char* name; +} SortStrategy; + +// Bubble Sort Implementation +void bubble_sort(int* arr, int size) { + for (int i = 0; i < size - 1; i++) { + for (int j = 0; j < size - i - 1; j++) { + if (arr[j] > arr[j + 1]) { + int temp = arr[j]; + arr[j] = arr[j + 1]; + arr[j + 1] = temp; + } + } + } +} + +// Quick Sort Implementation +void quick_sort_recursive(int* arr, int low, int high) { + if (low < high) { + int pivot = arr[high]; + int i = low - 1; + for (int j = low; j < high; j++) { + if (arr[j] < pivot) { + i++; + int temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; + } + } + int temp = arr[i + 1]; + arr[i + 1] = arr[high]; + arr[high] = temp; + + quick_sort_recursive(arr, low, i); + quick_sort_recursive(arr, i + 2, high); + } +} + +void quick_sort(int* arr, int size) { + quick_sort_recursive(arr, 0, size - 1); +} + +// Context (nutzt Strategy) +typedef struct { + SortStrategy* strategy; +} Sorter; + +void sorter_set_strategy(Sorter* s, SortStrategy* strat) { + s->strategy = strat; +} + +void sorter_execute(Sorter* s, int* arr, int size) { + if (s->strategy) { + printf("Sorting with: %s\n", s->strategy->name); + s->strategy->sort(arr, size); + } +} + +// ============================================ +// Hauptprogramm +// ============================================ +int main() { + printf("=== Modul 8: Modularisierung ===\n\n"); + + // Demo: MVC-Architektur + printf("--- MVC Architecture Demo ---\n"); + + AccountController controller; + controller.service = banking_service_create(); + controller.repository = repository_create(); + + controller_create_account(&controller, "Alice", 1000.0); + controller_deposit(&controller, "Alice", 500.0); + controller_show_balance(&controller, "Alice"); + + free(controller.service); + free(controller.repository); + + // Demo: Strategy Pattern + printf("\n--- Strategy Pattern Demo ---\n"); + + int data1[] = {64, 34, 25, 12, 22, 11, 90}; + int data2[] = {64, 34, 25, 12, 22, 11, 90}; + int size = 7; + + SortStrategy bubble = {bubble_sort, "Bubble Sort"}; + SortStrategy quick = {quick_sort, "Quick Sort"}; + + Sorter sorter; + + sorter_set_strategy(&sorter, &bubble); + sorter_execute(&sorter, data1, size); + printf("Result: "); + for (int i = 0; i < size; i++) printf("%d ", data1[i]); + printf("\n"); + + sorter_set_strategy(&sorter, &quick); + sorter_execute(&sorter, data2, size); + printf("Result: "); + for (int i = 0; i < size; i++) printf("%d ", data2[i]); + printf("\n"); + + printf("\n=== Wichtige Konzepte ===\n"); + printf("1. Separation of Concerns: Model, View, Controller\n"); + printf("2. Dependency Injection: Service/Repository übergeben\n"); + printf("3. Interface (struct mit Function Pointers) = Abstraktion\n"); + printf("4. Strategy Pattern: Austauschbare Algorithmen\n"); + printf("5. Modular = Testbar, Wartbar, Erweiterbar\n"); + + return 0; +}