桥接模式
Last updated
Last updated
package bridge
// 账户
type Account interface {
openAccount() Account
showAccountType()
}package bridge
type Bank interface {
openAccount() Account
}package bridge
import "fmt"
type DepositAccount struct {
}
func NewDepositAccount() *DepositAccount {
return &DepositAccount{}
}
func (DepositAccount) openAccount() Account {
fmt.Println("打开定期账号")
return *NewDepositAccount()
}
func (DepositAccount) showAccountType() {
fmt.Println("这是一个定期账号")
}package bridge
import "fmt"
type SavingAccount struct {
}
func NewSavingAccount() *SavingAccount {
return &SavingAccount{}
}
func (SavingAccount) openAccount() Account {
fmt.Println("打开活期账号")
return *NewSavingAccount()
}
func (SavingAccount) showAccountType() {
fmt.Println("这是一个活期账号")
}package bridge
import "fmt"
type ABCBank struct {
account Account
}
func NewABCBank(account Account) *ABCBank {
return &ABCBank{account: account}
}
func (abc ABCBank) openAccount() Account {
fmt.Println("打开中国农业银行账号")
abc.account.openAccount()
return abc.account
}package bridge
import "fmt"
type ICBCBank struct {
account Account
}
func NewICBCBank(account Account) *ICBCBank {
return &ICBCBank{account: account}
}
func (icbc ICBCBank) openAccount() Account {
fmt.Println("打开中国工商银行账号")
icbc.account.openAccount()
return icbc.account
}package bridge
import "testing"
func Test(t *testing.T) {
var icbcBank Bank = NewICBCBank(NewDepositAccount())
icbcAccount := icbcBank.openAccount()
icbcAccount.showAccountType()
var abcBank Bank = NewABCBank(NewSavingAccount())
abcAccount := abcBank.openAccount()
abcAccount.showAccountType()
}package tech.selinux.design.pattern.structural.bridge;
public interface Account {
Account openAccount();
void showAccountType();
}package tech.selinux.design.pattern.structural.bridge;
public abstract class Bank {
protected Account account;
public Bank(Account account) {
this.account = account;
}
abstract Account openAccount();
}package tech.selinux.design.pattern.structural.bridge;
public class DepositAccount implements Account {
@Override
public Account openAccount() {
System.out.println("打开定期账号");
return new DepositAccount();
}
@Override
public void showAccountType() {
System.out.println("这是一个定期账号");
}
}package tech.selinux.design.pattern.structural.bridge;
public class SavingAccount implements Account {
@Override
public Account openAccount() {
System.out.println("打开活期账号");
// ...
return new SavingAccount();
}
@Override
public void showAccountType() {
System.out.println("这是一个活期账号");
}
}package tech.selinux.design.pattern.structural.bridge;
public class ABCBank extends Bank {
public ABCBank(Account account) {
super(account);
}
@Override
Account openAccount() {
System.out.println("打开中国农业银行账号");
account.openAccount();
return account;
}
}package tech.selinux.design.pattern.structural.bridge;
public class ICBCBank extends Bank {
public ICBCBank(Account account) {
super(account);
}
@Override
Account openAccount() {
System.out.println("打开中国工商银行账号");
account.openAccount();
return account;
}
}package tech.selinux.design.pattern.structural.bridge;
public class Test {
public static void main(String[] args) {
Bank icbcBank = new ICBCBank(new DepositAccount());
Account icbcAccount = icbcBank.openAccount();
icbcAccount.showAccountType();
Bank icbcBank2 = new ICBCBank(new SavingAccount());
Account icbcAccount2 = icbcBank2.openAccount();
icbcAccount2.showAccountType();
Bank abcBank = new ABCBank(new SavingAccount());
Account abcAccount = abcBank.openAccount();
abcAccount.showAccountType();
}
}