Bridge mode of design mode, the relationship between computer and game

foreword

This article mainly talks about the bridging mode , and uses easy-to-understand cases in the article, so that you can better learn the knowledge points of this chapter and understand the principles, so that you can do it wisely.

1. What is bridge mode

The bridge mode is one of the structural modes among the 23 design modes . It puts the implementation and the abstraction in two different class levels, so that the two levels can be changed independently to achieve the decoupling of the two and maintain the integrity of each part. independence as well as their functional extension.

2. Bridging mode in life

1. Computer and games

Among the computers we use, there are desktop computers, laptop computers, and tablet computers. And we can play the same game on different computers. In the future, computers may have other types of computers, and games will be updated and iterated. The relationship between computers and games is like a bridge model, which develops independently but can be combined. .
insert image description here

2. Mobile phone and software

Our current mobile phones have all kinds of mobile phones, and the software also has various software. Different software can exist in a mobile phone at the same time, and the same software can also exist in different mobile phones. Software and mobile phones are what we will follow next. The bridging mode is the same, it develops independently but can be combined.

3. Model and brand

Today's cars are more diversified than before. In the past, there were only diesel cars and gasoline cars. Now there are not only pure electric cars but also hybrid cars. Perhaps solar cars may appear in the future. And there are more and more car brands than before, and there are different models of cars for different brands, and cars of the same model also have different brands. The relationship between the brand and model of a car is like a bridge model, two separate and independent dimensions can be combined.

3. Implementation of bridge mode

Next, we take the case of the relationship between the computer and the game, and implement it through the bridge mode. First create an implementation class interface role computer game interface and two concrete implementation class role game classes

package com.qianfeng.ran;

/*
 * @author:江帅
 *      实现类接口
 *          电脑游戏接口
 */
public interface ComputerGame {
    
    
    /**
     *  被下载的行为      --  供抽象实现角色调用
     * @param computer 具体的电脑类型
     */
    void beDownload(String computer);
}



/*
 * @author:江帅
 *      具体实现化角色
 *          Dota2 游戏类
 */
class DOTA2 implements ComputerGame{
    
    

    @Override
    public void beDownload(String computer) {
    
    
        System.out.println("使用"+computer+"玩 Dota2");
    }
}



/*
 * @author:江帅
 *      具体实现化角色
 *          LOL 游戏类
 */
class LOL implements ComputerGame{
    
    
    @Override
    public void beDownload(String computer) {
    
    
        System.out.println("使用"+computer+"玩 LOL");
    }
}

Next, create an abstract computer class and an abstract implementation class computer class with a specific model

package com.qianfeng.ran;

/*
 * @author:江帅
 *      抽象化类
 *          电脑类
 */
public abstract class Computer {
    
    
    //游戏类对象
    public ComputerGame computerGame;
    public void setComputerGame(ComputerGame computerGame) {
    
    
        this.computerGame = computerGame;
    }
    /**
     * 玩游戏行为
     */
    public abstract void playGame();
}



/*
 * @author:江帅
 *      抽象化实现类
 *          台式电脑类
 */
class DesktopComputer extends Computer{
    
    
    @Override
    public void playGame() {
    
    
        //下载游戏
        computerGame.beDownload("台式电脑");
    }
}



/*
 * @author:江帅
 *      抽象化实现类
 *          笔记本电脑类
 */
class LaptopComputer extends Computer {
    
    
    @Override
    public void playGame() {
    
    
        //下载游戏
        computerGame.beDownload("笔记本电脑");
    }
}

Finally, through the bridge mode, different types of computers can be used to play different games

package com.qianfeng.ran.;

/*
 * @author:江帅
 *          客户端
 */
public class Demo {
    
    
    public static void main(String[] args) {
    
    
        //创建 DOTA2 游戏对象
        ComputerGame dota2 = new DOTA2();
        //创建 LOL 游戏对象
        ComputerGame lol = new LOL();

        //--------------使用台式电脑玩dota2-------
        //创建台式电脑对象
        Computer computer = new DesktopComputer();
        //下载游戏		--		台式电脑对象添加 DOTA2 游戏对象
        computer.setComputerGame(dota2);
        //执行结果:
        //使用台式电脑玩 Dota2
        computer.playGame();

        //--------------使用台式电脑玩lol-------
        //下载游戏		--		台式电脑对象添加 LOL 游戏对象
        computer.setComputerGame(lol);
        //执行结果:
        //使用台式电脑玩 LOL
        computer.playGame();

        //--------------使用笔记本电脑玩dota2-------
        //创建笔记本电脑
        computer = new LaptopComputer();
        //下载游戏		--		笔记本电脑对象添加 DOTA2 游戏对象
        computer.setComputerGame(dota2);
        //执行结果:
        //使用笔记本电脑玩 Dota2
        computer.playGame();

        //--------------使用笔记本电脑玩lol-------
        //下载游戏		--		笔记本电脑对象添加 LOL 游戏对象
        computer.setComputerGame(lol);
        //执行结果:
        //使用笔记本电脑玩 LOL
        computer.playGame();
    }
}

Four. Summary

The bridge mode is suitable for use when a class has two independently changing dimensions, and both dimensions need to be extended and not extended through inheritance.

The generation of subclasses is reduced, and the cost of management and maintenance is reduced. However, it will increase the difficulty of system design, and it is necessary to identify changes in two independent dimensions.

In the next chapter, we will take you to learn the development of new energy (adapter mode of design mode)


Guess you like

Origin blog.csdn.net/GUDUzhongliang/article/details/131303545