Skip to content

Commit dabd596

Browse files
committed
Facade pattern
1 parent 4661e28 commit dabd596

File tree

9 files changed

+174
-0
lines changed

9 files changed

+174
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ members = [
1313
"structural/bridge",
1414
"structural/composite",
1515
"structural/decorator",
16+
"structural/facade",
1617
]

structural/facade/Cargo.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "facade"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
10+
[[bin]]
11+
name = "facade"
12+
path = "main.rs"

structural/facade/account.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
pub struct Account {
2+
name: String,
3+
}
4+
5+
impl Account {
6+
pub fn new(name: String) -> Self {
7+
Self { name }
8+
}
9+
10+
pub fn check(&self, name: &String) -> Result<(), String> {
11+
if &self.name != name {
12+
return Err("Account name is incorrect".into());
13+
}
14+
15+
println!("Account verified");
16+
Ok(())
17+
}
18+
}
19+
20+
// func (a *Account) checkAccount(accountName string) error {
21+
// if a.name != accountName {
22+
// return fmt.Errorf("Account Name is incorrect")
23+
// }
24+
// fmt.Println("Account Verified")
25+
// return nil
26+
// }

structural/facade/ledger.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
pub struct Ledger;
2+
3+
impl Ledger {
4+
pub fn make_entry(&mut self, account_id: &String, txn_type: String, amount: u32) {
5+
println!(
6+
"Make ledger entry for accountId {} with transaction type {} for amount {}",
7+
account_id, txn_type, amount
8+
);
9+
}
10+
}

structural/facade/main.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
mod account;
2+
mod ledger;
3+
mod notification;
4+
mod security_code;
5+
mod wallet;
6+
mod wallet_facade;
7+
8+
use wallet_facade::WalletFacade;
9+
10+
fn main() -> Result<(), String> {
11+
let mut wallet = WalletFacade::new("abc".into(), 1234);
12+
println!("");
13+
14+
wallet.add_money_to_wallet(&"abc".into(), 1234, 10)?;
15+
println!("");
16+
17+
wallet.deduct_money_from_wallet(&"abc".into(), 1234, 5)
18+
}

structural/facade/notification.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
pub struct Notification;
2+
3+
impl Notification {
4+
pub fn send_wallet_credit_notification(&self) {
5+
println!("Sending wallet credit notification");
6+
}
7+
8+
pub fn send_wallet_debit_notification(&self) {
9+
println!("Sending wallet debit notification");
10+
}
11+
}

structural/facade/security_code.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
pub struct SecurityCode {
2+
code: u32,
3+
}
4+
5+
impl SecurityCode {
6+
pub fn new(code: u32) -> Self {
7+
Self { code }
8+
}
9+
10+
pub fn check(&self, code: u32) -> Result<(), String> {
11+
if self.code != code {
12+
return Err("Security code is incorrect".into());
13+
}
14+
15+
println!("Security code verified");
16+
Ok(())
17+
}
18+
}

structural/facade/wallet.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
pub struct Wallet {
2+
balance: u32,
3+
}
4+
5+
impl Wallet {
6+
pub fn new() -> Self {
7+
Self { balance: 0 }
8+
}
9+
10+
pub fn credit_balance(&mut self, amount: u32) {
11+
self.balance += amount;
12+
}
13+
14+
pub fn debit_balance(&mut self, amount: u32) {
15+
self.balance
16+
.checked_sub(amount)
17+
.expect("Balance is not sufficient");
18+
}
19+
}

structural/facade/wallet_facade.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
use crate::{
2+
account::Account, ledger::Ledger, notification::Notification, security_code::SecurityCode,
3+
wallet::Wallet,
4+
};
5+
6+
pub struct WalletFacade {
7+
account: Account,
8+
wallet: Wallet,
9+
code: SecurityCode,
10+
notification: Notification,
11+
ledger: Ledger,
12+
}
13+
14+
impl WalletFacade {
15+
pub fn new(account_id: String, code: u32) -> Self {
16+
println!("Starting create account");
17+
18+
let this = Self {
19+
account: Account::new(account_id),
20+
wallet: Wallet::new(),
21+
code: SecurityCode::new(code),
22+
notification: Notification,
23+
ledger: Ledger,
24+
};
25+
26+
println!("Account created");
27+
this
28+
}
29+
30+
pub fn add_money_to_wallet(
31+
&mut self,
32+
account_id: &String,
33+
security_code: u32,
34+
amount: u32,
35+
) -> Result<(), String> {
36+
println!("Starting add money to wallet");
37+
self.account.check(account_id)?;
38+
self.code.check(security_code)?;
39+
self.wallet.credit_balance(amount);
40+
self.notification.send_wallet_credit_notification();
41+
self.ledger.make_entry(account_id, "credit".into(), amount);
42+
Ok(())
43+
}
44+
45+
pub fn deduct_money_from_wallet(
46+
&mut self,
47+
account_id: &String,
48+
security_code: u32,
49+
amount: u32,
50+
) -> Result<(), String> {
51+
println!("Starting debit money from wallet");
52+
self.account.check(account_id)?;
53+
self.code.check(security_code)?;
54+
self.wallet.debit_balance(amount);
55+
self.notification.send_wallet_debit_notification();
56+
self.ledger.make_entry(account_id, "debit".into(), amount);
57+
Ok(())
58+
}
59+
}

0 commit comments

Comments
 (0)