Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Three examples: what is bridge mode, why is it used, how is it used, and the code to implement it

An overview of the

Take bridge mode for example

1, a soldier, at the beginning of the only division, that is, infantry or cavalry. After A certain amount of meritorious service, soldiers are assigned grades A, B, and C

2. Coffee, in the beginning, we only need to pay attention to whether it is a large or medium cup. After selling for some time, I found that some people like to add sugar and some people like to add milk, so I added the properties of sugar, milk, nothing

3, painting, at the beginning I only pay attention to whether I draw a circle or a five-pointed star. But the teacher said to be lively, so added colors red, yellow, blue

What is bridge mode

Structural mode, abstraction and realization of decoupling, by bridging the two to complete the decoupling of the two

Why use bridge mode

Look at the big guy example (soldier)

There are two stats for soldiers – level and type. So if I add another unit “special forces”, do I need to add three implementations, i.e., A special Force, B special Forces, and C special forces?

(coffee), if I add the small, do I add the small with nothing, the small with sugar, the small with milk

(painting), if add triangle, do add red triangle, yellow triangle, blue triangle

Doesn’t that seem too redundant

If these attributes are decoupled from each other, i.e., troop class is a separate attribute and soldier level is a single attribute, you only need to arrange and combine them when implementing objects rather than generating concrete implementation classes.

When to use bridge mode

Multiple attributes identify the same abstract class and can be arranged and combined to produce concrete implementation classes

implementation

Logic implementation

1. An abstract class of concrete needs, such as soldiers, coffee, painting results; And its construction method, and the logical method that the business needs

2. The original abstract class realizes the object (engineer, big cup of coffee, a round painting), and realizes the abstract class and its concrete business logic

3, new interface, such as (soldier rank, sugar, color), and the interface needs method constraints

4. The implementation class of the interface, for example (level A, sweetened, red), and its specific business implementation

5. Call

  • Implement the new implementation class
  • Create a concrete implementation object for the abstract class
  • Invoke the concrete implementation method

Code implementation (soldier level and job type)

1. Create a soldier abstract class (attributes of the abstract class include rank and name)

* * *@ClassName Soldier
 * @DescAbstract soldier class (most important) *1. Abstracts the characteristics required by a soldier *2. Bridge the nature of the soldier (infantry, cavalry) to the level of the soldier (A, B, C) *@Author YangMingYu
 * @Date 2021/10/27 10:50On the morning of *@Version 1.0* * /public abstract class Soldier {
    
    private String name;
    Rank rank;

    public Soldier(Rank rank, String name) {
        this.rank = rank;
        this.name = name;
    }

    public String getName(a) {
        return name;
    }

    public abstract void type(a);
}
Copy the code
  1. Create soldier implementation classes (infantry, cavalry, archers)
/ * * *@ClassName Infantry
 * @DescThe infantry *@Author YangMingYu
 * @Date 2021/10/27 10:53 上午
 * @Version1.0 * * /
public class Infantry extends Soldier {
    public Infantry(Rank rank, String name) {
        super(rank, name);
    }

    @Override
    public void type(a) {
        // express the nature of the troop
        System.out.print("I'm a foot soldier." + this.getName() + ",");
        // Express troop levelrank.rankByAchieve(); }}/ * * *@ClassName Cavalry
 * @DescCavalry *@Author YangMingYu
 * @Date 2021/10/27 10:56 上午
 * @Version1.0 * * /
public class Cavalry extends Soldier {
    public Cavalry(Rank rank, String name) {
        super(rank, name);
    }
    @Override
    public void type(a) {
        // express the nature of the troop
        System.out.print("I'm a foot soldier." + this.getName() + ",");
        // Express troop levelrank.rankByAchieve(); }}/ * * *@ClassName Bowman
 * @DescArcher *@Author YangMingYu
 * @Date 2021/10/27 10:56 上午
 * @Version1.0 * * /
public class Bowman extends Soldier {
    public Bowman(Rank rank, String name) {
        super(rank, name);
    }

    @Override
    public void type(a) {
        // express the nature of the troop
        System.out.print("I'm an archer." + this.getName() + ",");
        // Express troop levelrank.rankByAchieve(); }}Copy the code
  1. Create level interfaces
/ * * *@ClassName Rank
 * @DescHierarchical interface@Author YangMingYu
 * @Date 2021/10/27 10:51 上午
 * @Version1.0 * * /
public interface Rank {
    void rankByAchieve(a); // Grades are based on achievement
}
Copy the code
  1. Create a level of implementation class
/ * * *@ClassName RankForA
 * @DescSuch as A *@Author YangMingYu
 * @Date 2021/10/27 10:52 上午
 * @Version1.0 * * /
public class RankForA implements Rank {


    @Override
    public void rankByAchieve(a) {
        System.out.println("Grade A"...); }}/ * * *@ClassName RankForA
 * @DescB, etc. *@Author YangMingYu
 * @Date 2021/10/27 10:52 上午
 * @Version1.0 * * /
public class RankForB implements Rank {


    @Override
    public void rankByAchieve(a) {
        System.out.println("Grade B..."); }}/ * * *@ClassName RankForA
 * @DescC *@Author YangMingYu
 * @Date 2021/10/27 10:52 上午
 * @Version1.0 * * /
public class RankForC implements Rank {


    @Override
    public void rankByAchieve(a) {
        System.out.println("Grade C..."); }}Copy the code
  1. Generates a soldier object and calls a method to describe itself
/ * * *@ClassName BridgingDemo
 * @DescUse of bridge objects * 1. The bridged property implementation class object (new property) * 2. The implementation object (original) that Bridges another property * 3. Call the corresponding method * of the implementation object generated by the original property@Author YangMingYu
 * @Date 2021/10/27 10:57 上午
 * @Version1.0 * * /
public class BridgingDemo {

    public static void main(String[] args) {
        Rank rankA = new RankForA();
        Infantry infantry = new Infantry(rankA, "xiaoMing"); / / the infantry
        infantry.type();
        Cavalry cavalry = new Cavalry(rankA, "xiaoWu"); / / the cavalry
        cavalry.type();

        Rank rankB = new RankForB();
        Infantry infantry2 = new Infantry(rankB, "xiaoLi"); / / the infantryinfantry2.type(); }}Copy the code

Learning from:

  1. Starry- The Big Man and The Grandiose design pattern
  2. Novice tutorial