Structural model - bridge mode (VI)

The project source address: https: //github.com/ggb2312/JavaNotes/tree/master/design-pattern (design patterns relevant code and notes)

1. Definitions

The abstraction from its concrete realization section. So that they can vary independently. Establish links between two classes by means of a combination, rather than inheritance.

2. applicable

  • Add more flexibility between the abstract and the concrete realization
  • A class there are two (or more) dimensions vary independently. And the two (or more) dimensions need to be extended independently
  • Without wishing to use inheritance, or because the number of the multilayer system leading to inherited class surge

3. The class diagram and role

Class Diagram

  • Abstraction is the role of abstraction, define the behavior of the characters, while preserving a reference to the realization of the role;
  • Implementor achieve the role of which is the interface or abstract class that defines the role of the required properties and behavior;
  • RefinedAbstraction as amended abstract role, the role of the reference implementation of the abstract roles amended;
  • ConcreteImplementor concrete realization of the role, implement the interface or abstract class defines methods or properties.

4. The relevant design patterns

Bridging mode and a combined mode

  • Combined mode is emphasized that the combination between the parts and the whole, be emphasized that the bridge mode in parallel levels, combinations of different classes of

Bridge mode and the adapter mode

  • Adapter change the interface mode, abstract and concrete bridge mode separation.

Example 5. Mode

Background: The Chinese have a lot of banks, Agricultural Bank of China and Industrial and Commercial Bank of China on our account, current account and an account on a regular basis.

(1) bridge mode - achieve

Have an account interfaces:

public interface Account {
    /** 打开我们的账号,打开账号,就要返回账号 */
    Account openAccount();
    
    /** 打开我们的账号,查看为什么账户类型,是定期类型还是活期类型 */
    void showAccountType();

}

A regular account:

/** 定期的账号 */
public class DepositAccount implements Account {
    @Override
    public Account openAccount() {
        System.out.println("定期账号");
        return new DepositAccount();
    }

    @Override
    public void showAccountType() {
        System.out.println("这是一个定期账号");
    }
}

A current account:

/** 活期账号 */
public class SavingAccount implements Account {
    @Override
    public Account openAccount() {
        System.out.println("打开活期账号");
        return new SavingAccount();
    }

    @Override
    public void showAccountType() {
        System.out.println("这是一个活期账号");
    }
}

(2) bridge mode - Abstract

The Account Account interface combinations to the bank:

public abstract class Bank {
    /** 只有子类能拿到这个Account的这个接口 */
    protected Account account;

    /** 组合的时候,可以通过构造器的方式来进行注入也可以通过set方法的方式来进行注入 */
    public Bank(Account account) {
        this.account = account;
    }

    /** 这里声明成和接口里面的方法名一致,只是方便理解,Bank里面的方法要委托给Account接口里面的方法 */
    abstract Account openAccount();

}

We now see that Account is a specific implementation, we need to use the specific implementation of this interface. Bank is an abstract class, and then commissioned an abstract class behavior inside this interface to the Account.
Speaking in front bridging mode refers to the abstraction and separation; here implemented herein Account formal implementation class.

Now the bank interface has two subclasses to inherit it.

China Agricultural Bank of class:

public class ABCBank extends Bank {
    /**
     * 组合的时候,可以通过构造器的方式来进行注入也可以通过set方法的方式来进行注入
     *
     * @param account
     */
    public ABCBank(Account account) {
        super(account);
    }

    @Override
    Account openAccount() {
        System.out.println("打开中国农业银行账号");
         //将行为委托给account
        account.openAccount();
        return account;
    }
}

Commercial Bank of China account:

public class ICBCBank extends Bank {
    /**
     * 组合的时候,可以通过构造器的方式来进行注入也可以通过set方法的方式来进行注入
     *
     * @param account
     */
    public ICBCBank(Account account) {
        super(account);
    }

    @Override
    Account openAccount() {
        System.out.println("打开中国工商银行账号");
        //将行为委托给account
        account.openAccount();
        return account;
    }
}

(3) Test Class

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();
    }
}

Test Results:

Test Results

(4) FIG class

Abstract and can be peeled off, in combination introducing the Account Bank, Bank in the abstract, are implemented in Acount. The required functions entrusted to implement.

Class Diagram

6. pros and cons

advantage:

  • Separating the abstract part and embodied portion
  • Improve the system scalability
  • In line with the principle of opening and closing
  • In line with the principle of multiplex synthesis

Disadvantages:

  • Increase the difficulty of understanding the design of the system
  • We need to correctly recognize two independent dimensions change system

7. Extended -JDK1.7 source and a frame in bridge mode

7.1 java.sql.Driver, java.sql.DriverManager and java.sql.DriverInfo
MySQL's Driver, oracle is to realize part of Driver bridge mode.

7.2 DriverManager and DriverInfo (ie Driver) realized the bridge mode

Guess you like

Origin www.cnblogs.com/gj-blog/p/10929510.html