用桥接模式实现银行账户管理
时间: 2023-07-18 07:10:55 浏览: 121
好的,我来回答你的问题。
首先,让我们来了解一下桥接模式。桥接模式是一种结构型设计模式,它将抽象部分与实现部分分离,使它们可以独立地变化。
在银行账户管理中,我们可以将抽象部分定义为账户类型,实现部分定义为账户操作。具体来说,我们可以定义一个抽象类 `Account`,其中包含了账户的基本属性和方法,如账户名、账户余额、存款、取款等。然后,我们可以定义不同的账户类型,如储蓄账户、信用卡账户等,它们继承自 `Account` 类,并实现自己特有的方法和属性。
接下来,我们需要定义实现部分。我们可以定义一个接口 `AccountOperation`,其中包含了账户操作的方法,如存款、取款等。然后,我们可以实现这个接口,定义不同的账户操作类,如 `SavingAccountOperation`、`CreditCardAccountOperation` 等。这些类实现了 `AccountOperation` 接口,并具有自己特有的实现方式。
最后,我们需要将抽象部分和实现部分桥接起来。我们可以定义一个类 `AccountBridge`,其中包含了一个 `Account` 对象和一个 `AccountOperation` 对象。这个类可以实现 `Account` 中的方法,然后调用 `AccountOperation` 中的具体实现。这样,我们就可以通过 `AccountBridge` 来管理不同类型的账户,同时实现不同的账户操作。
下面是一个简单的示例代码:
```java
// 抽象类 Account
public abstract class Account {
protected String accountName;
protected double balance;
public Account(String accountName, double balance) {
this.accountName = accountName;
this.balance = balance;
}
public abstract void deposit(double amount);
public abstract void withdraw(double amount);
}
// 储蓄账户类 SavingAccount
public class SavingAccount extends Account {
public SavingAccount(String accountName, double balance) {
super(accountName, balance);
}
@Override
public void deposit(double amount) {
balance += amount;
System.out.println("Deposit " + amount + " to saving account " + accountName);
}
@Override
public void withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
System.out.println("Withdraw " + amount + " from saving account " + accountName);
} else {
System.out.println("Insufficient balance in saving account " + accountName);
}
}
}
// 信用卡账户类 CreditCardAccount
public class CreditCardAccount extends Account {
public CreditCardAccount(String accountName, double balance) {
super(accountName, balance);
}
@Override
public void deposit(double amount) {
balance += amount;
System.out.println("Deposit " + amount + " to credit card account " + accountName);
}
@Override
public void withdraw(double amount) {
double limit = balance * 0.9; // 信用卡账户只能取 90% 的余额
if (limit >= amount) {
balance -= amount;
System.out.println("Withdraw " + amount + " from credit card account " + accountName);
} else {
System.out.println("Exceeded limit in credit card account " + accountName);
}
}
}
// 接口 AccountOperation
public interface AccountOperation {
void deposit(double amount);
void withdraw(double amount);
}
// 储蓄账户操作类 SavingAccountOperation
public class SavingAccountOperation implements AccountOperation {
private SavingAccount account;
public SavingAccountOperation(SavingAccount account) {
this.account = account;
}
@Override
public void deposit(double amount) {
account.deposit(amount);
}
@Override
public void withdraw(double amount) {
account.withdraw(amount);
}
}
// 信用卡账户操作类 CreditCardAccountOperation
public class CreditCardAccountOperation implements AccountOperation {
private CreditCardAccount account;
public CreditCardAccountOperation(CreditCardAccount account) {
this.account = account;
}
@Override
public void deposit(double amount) {
account.deposit(amount);
}
@Override
public void withdraw(double amount) {
account.withdraw(amount);
}
}
// 类 AccountBridge
public class AccountBridge {
private Account account;
private AccountOperation operation;
public AccountBridge(Account account, AccountOperation operation) {
this.account = account;
this.operation = operation;
}
public void deposit(double amount) {
operation.deposit(amount);
}
public void withdraw(double amount) {
operation.withdraw(amount);
}
}
// 测试代码
public class Test {
public static void main(String[] args) {
SavingAccount savingAccount = new SavingAccount("John", 10000);
SavingAccountOperation savingAccountOperation = new SavingAccountOperation(savingAccount);
AccountBridge savingAccountBridge = new AccountBridge(savingAccount, savingAccountOperation);
CreditCardAccount creditCardAccount = new CreditCardAccount("Mary", 20000);
CreditCardAccountOperation creditCardAccountOperation = new CreditCardAccountOperation(creditCardAccount);
AccountBridge creditCardAccountBridge = new AccountBridge(creditCardAccount, creditCardAccountOperation);
savingAccountBridge.deposit(5000);
savingAccountBridge.withdraw(2000);
creditCardAccountBridge.deposit(10000);
creditCardAccountBridge.withdraw(25000);
}
}
```
这个示例代码使用了桥接模式来实现银行账户管理。其中,`Account` 类是抽象部分,`SavingAccount` 和 `CreditCardAccount` 类是具体的账户类型。`AccountOperation` 接口是实现部分,`SavingAccountOperation` 和 `CreditCardAccountOperation` 类是具体的账户操作。`AccountBridge` 类将抽象部分和实现部分桥接起来。通过 `AccountBridge` 类,我们可以管理不同类型的账户,同时进行不同的账户操作。
阅读全文