桥接模式
This commit is contained in:
@@ -0,0 +1,22 @@
|
|||||||
|
package top.fjy8018.designpattern.pattern.structural.bridge;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 银行账号——行为接口
|
||||||
|
*
|
||||||
|
* @author F嘉阳
|
||||||
|
* @date 2020/2/29 9:55
|
||||||
|
*/
|
||||||
|
public interface Account {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 打开一个账号
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
Account openAccount();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查看账号类型
|
||||||
|
*/
|
||||||
|
void showAccountType();
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
package top.fjy8018.designpattern.pattern.structural.bridge;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 抽象银行
|
||||||
|
* 组合账户接口,交由子类实现具体的行为
|
||||||
|
*
|
||||||
|
* @author F嘉阳
|
||||||
|
* @date 2020/2/29 10:05
|
||||||
|
*/
|
||||||
|
@AllArgsConstructor
|
||||||
|
public abstract class Bank {
|
||||||
|
|
||||||
|
protected Account account;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 打开一个账户
|
||||||
|
* 该方法定义与接口相同,方法名称可以不同
|
||||||
|
* 当前抽象类的openAccount方法需要委托给Account接口openAccount方法实现
|
||||||
|
* 当前类为抽象类,委托给接口的实现类执行,体现抽象与实现分离
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
abstract Account openAccount();
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
package top.fjy8018.designpattern.pattern.structural.bridge;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 定期账户
|
||||||
|
*
|
||||||
|
* @author F嘉阳
|
||||||
|
* @date 2020/2/29 9:57
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
public class DepositAccount implements Account {
|
||||||
|
/**
|
||||||
|
* 打开一个账号
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Account openAccount() {
|
||||||
|
log.info("打开定期账户");
|
||||||
|
return new DepositAccount();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查看账号类型
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void showAccountType() {
|
||||||
|
log.info("这是一个定期账户");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
package top.fjy8018.designpattern.pattern.structural.bridge;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author F嘉阳
|
||||||
|
* @date 2020/2/29 10:09
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
public class GCBBank extends Bank {
|
||||||
|
|
||||||
|
public GCBBank(Account account) {
|
||||||
|
super(account);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 打开一个账户
|
||||||
|
* 该方法定义与接口相同,方法名称可以不同
|
||||||
|
* 当前抽象类的openAccount方法需要委托给Account接口openAccount方法实现
|
||||||
|
* 当前类为抽象类,委托给接口的实现类执行,体现抽象与实现分离
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
Account openAccount() {
|
||||||
|
log.info("打开广州银行账户");
|
||||||
|
// 委托
|
||||||
|
account.openAccount();
|
||||||
|
return account;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
package top.fjy8018.designpattern.pattern.structural.bridge;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author F嘉阳
|
||||||
|
* @date 2020/2/29 10:11
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
public class ICBCBank extends Bank {
|
||||||
|
|
||||||
|
public ICBCBank(Account account) {
|
||||||
|
super(account);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 打开一个账户
|
||||||
|
* 该方法定义与接口相同,方法名称可以不同
|
||||||
|
* 当前抽象类的openAccount方法需要委托给Account接口openAccount方法实现
|
||||||
|
* 当前类为抽象类,委托给接口的实现类执行,体现抽象与实现分离
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
Account openAccount() {
|
||||||
|
log.info("打开工商银行账户");
|
||||||
|
// 委托
|
||||||
|
account.openAccount();
|
||||||
|
return account;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
package top.fjy8018.designpattern.pattern.structural.bridge;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 活期账户
|
||||||
|
*
|
||||||
|
* @author F嘉阳
|
||||||
|
* @date 2020/2/29 9:58
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
public class SavingAccount implements Account {
|
||||||
|
/**
|
||||||
|
* 打开一个账号
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Account openAccount() {
|
||||||
|
log.info("打开活期账户");
|
||||||
|
return new SavingAccount();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查看账号类型
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void showAccountType() {
|
||||||
|
log.info("这是一个活期账户");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
package top.fjy8018.designpattern.pattern.structural.bridge;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author F嘉阳
|
||||||
|
* @date 2020/2/29 14:30
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
class AccountTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void openAccount() {
|
||||||
|
Bank gcbBank = new GCBBank(new DepositAccount());
|
||||||
|
Account gcbAccount = gcbBank.openAccount();
|
||||||
|
gcbAccount.showAccountType();
|
||||||
|
|
||||||
|
Bank icbcBank = new ICBCBank(new SavingAccount());
|
||||||
|
Account icbcAccount = icbcBank.openAccount();
|
||||||
|
icbcAccount.showAccountType();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user