feat: reorganize workbook into 3 tracks (core, engineering-2026, ecosystems)
This commit is contained in:
1 parent
82677455b2
commit
2e002b6176
64 files changed
+2295
-196
No files matched your search
@@ -0,0 +1,4 @@
|
||||
[package]
|
||||
name = "core08-oop-composition"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
@@ -0,0 +1,16 @@
|
||||
# 08 — Obiektowość po Rustowemu (Kompozycja zamiast Dziedziczenia)
|
||||
|
||||
Rust celowo **nie posiada dziedziczenia klas** (`extends`). Zamiast tego łączy:
|
||||
* **enkapsulację** (prywatne pola + publiczne metody),
|
||||
* **kompozycję** (struktura posiada inne struktury),
|
||||
* **polimorfizm oparty o traity** (zarówno statyczny, jak i dynamiczny `Box<dyn Trait>`).
|
||||
|
||||
## Zadania (`src/lib.rs`)
|
||||
1. `BankAccount` — enkapsulacja salda (`balance` prywatne): `new`, `deposit`, `withdraw` (zwraca `bool`), `balance`.
|
||||
2. `Notifier` trait z domyślną metodą `prefix(&self) -> &str` oraz implementacje `EmailNotifier` i `SmsNotifier`.
|
||||
3. `AlertService` — kompozycja z heterogeniczną listą `Vec<Box<dyn Notifier>>`: metoda `broadcast(&self, msg: &str) -> Vec<String>`.
|
||||
|
||||
## Uruchomienie testów
|
||||
```bash
|
||||
make test PKG=core08-oop-composition
|
||||
```
|
||||
@@ -0,0 +1,100 @@
|
||||
#![allow(unused_variables, dead_code)]
|
||||
|
||||
pub struct BankAccount {
|
||||
balance: u64,
|
||||
}
|
||||
|
||||
impl BankAccount {
|
||||
pub fn new(initial_balance: u64) -> Self {
|
||||
todo!("Zainicjalizuj konto z initial_balance")
|
||||
}
|
||||
|
||||
pub fn deposit(&mut self, amount: u64) {
|
||||
todo!("Dodaj amount do salda")
|
||||
}
|
||||
|
||||
pub fn withdraw(&mut self, amount: u64) -> bool {
|
||||
todo!("Wypłać amount jeśli balance >= amount i zwróć true; inaczej false")
|
||||
}
|
||||
|
||||
pub fn balance(&self) -> u64 {
|
||||
todo!("Zwróć aktualne saldo")
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Notifier {
|
||||
fn channel(&self) -> &str;
|
||||
|
||||
fn format_message(&self, msg: &str) -> String {
|
||||
format!("[{}] {}", self.channel(), msg)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct EmailNotifier {
|
||||
pub address: String,
|
||||
}
|
||||
|
||||
impl Notifier for EmailNotifier {
|
||||
fn channel(&self) -> &str {
|
||||
todo!("Zwróć \"email\"")
|
||||
}
|
||||
}
|
||||
|
||||
pub struct SmsNotifier {
|
||||
pub phone: String,
|
||||
}
|
||||
|
||||
impl Notifier for SmsNotifier {
|
||||
fn channel(&self) -> &str {
|
||||
todo!("Zwróć \"sms\"")
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct AlertService {
|
||||
notifiers: Vec<Box<dyn Notifier>>,
|
||||
}
|
||||
|
||||
impl AlertService {
|
||||
pub fn new() -> Self {
|
||||
todo!("Utwórz pusty AlertService")
|
||||
}
|
||||
|
||||
pub fn register(&mut self, notifier: Box<dyn Notifier>) {
|
||||
todo!("Dodaj notifier do listy")
|
||||
}
|
||||
|
||||
pub fn broadcast(&self, msg: &str) -> Vec<String> {
|
||||
todo!("Wywołaj format_message(msg) na każdym notifierze i zbierz wyniki")
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn encapsulates_bank_account_balance() {
|
||||
let mut acc = BankAccount::new(100);
|
||||
acc.deposit(50);
|
||||
assert_eq!(acc.balance(), 150);
|
||||
assert!(acc.withdraw(40));
|
||||
assert_eq!(acc.balance(), 110);
|
||||
assert!(!acc.withdraw(500));
|
||||
assert_eq!(acc.balance(), 110);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn broadcasts_via_dynamic_dispatch() {
|
||||
let mut service = AlertService::new();
|
||||
service.register(Box::new(EmailNotifier {
|
||||
address: String::from("dev@example.com"),
|
||||
}));
|
||||
service.register(Box::new(SmsNotifier {
|
||||
phone: String::from("+48123456789"),
|
||||
}));
|
||||
|
||||
let sent = service.broadcast("Disk 90% full");
|
||||
assert_eq!(sent, vec!["[email] Disk 90% full", "[sms] Disk 90% full"]);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user