用JAVA桥接模式实现银行账户管理
时间: 2023-07-18 08:44:03 浏览: 99
好的,银行账户管理可以使用桥接模式来实现。首先,我们需要定义抽象类和接口,如下所示:
```java
// 定义账户接口
public interface Account {
void openAccount();
void showAccount();
}
// 定义银行抽象类,用于管理账户
public abstract class Bank {
protected Account account;
public Bank(Account account) {
this.account = account;
}
public abstract void openAccount();
public abstract void showAccount();
}
```
接下来我们可以定义具体的账户类型和银行类型,例如:
```java
// 定义储蓄账户
public class SavingsAccount implements Account {
@Override
public void openAccount() {
System.out.println("Open savings account");
}
@Override
public void showAccount() {
System.out.println("This is a savings account");
}
}
// 定义信用卡账户
public class CreditAccount implements Account {
@Override
public void openAccount() {
System.out.println("Open credit account");
}
@Override
public void showAccount() {
System.out.println("This is a credit account");
}
}
// 定义中国银行
public class ChinaBank extends Bank {
public ChinaBank(Account account) {
super(account);
}
@Override
public void openAccount() {
System.out.println("Open account in ChinaBank");
account.openAccount();
}
@Override
public void showAccount() {
System.out.println("This is ChinaBank");
account.showAccount();
}
}
// 定义工商银行
public class ICBCBank extends Bank {
public ICBCBank(Account account) {
super(account);
}
@Override
public void openAccount() {
System.out.println("Open account in ICBCBank");
account.openAccount();
}
@Override
public void showAccount() {
System.out.println("This is ICBCBank");
account.showAccount();
}
}
```
最后我们可以在客户端中使用这些类来管理银行账户:
```java
public class Client {
public static void main(String[] args) {
Account savingsAccount = new SavingsAccount();
Account creditAccount = new CreditAccount();
Bank chinaBank = new ChinaBank(savingsAccount);
chinaBank.openAccount();
chinaBank.showAccount();
Bank icbcBank = new ICBCBank(creditAccount);
icbcBank.openAccount();
icbcBank.showAccount();
}
}
```
这样就可以使用桥接模式来管理银行账户了。
阅读全文