设计模式--桥接模式

实验9:桥接模式

本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:

1、理解桥接模式的动机,掌握该模式的结构;

2、能够利用桥接模式解决实际问题。

实验任务:两个维度的桥接模式

用桥接模式实现在路上开车这个问题,其中,车可以是car或bus,路可以是水泥路或沥青路。

类图

源代码

java 复制代码
/**
 * 抽象汽车类
 */
public abstract class Vehicle
{
    protected Road road;

    //为汽车设置行驶的马路
    public void setRoad(Road road)
    {
        this.road = road;
    }

    //抽象行驶方法
    public abstract void drive();
}

public class Bus extends Vehicle
{
    //bus的行驶方法
    @Override
    public void drive()
    {
        String carType = "bus";
        this.road.bedrive(carType);
    }
}

public class Car extends Vehicle
{
    //car的行驶方法
    @Override
    public void drive()
    {
        String carType = "car";
        this.road.bedrive(carType);
    }
}

public interface Road
{
    void bedrive(String carType);
}

public class ConcreteRoad implements Road
{
    //水泥路
    @Override
    public void bedrive(String carType)
    {
        System.out.println(carType+"在水泥路行驶");
    }
}

public class PitchRoad implements Road
{
    //沥青路
    @Override
    public void bedrive(String carType)
    {
        System.out.println(carType + "在沥青路行驶");
    }
}

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;

public class XMLUtil
{
    public static Object getBean(String args)
    {
        try
        {
            DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = dFactory.newDocumentBuilder();
            Document doc;
            doc = builder.parse(new File("src/main/resources/config9.xml"));
            Node classNode = null;
            NodeList nl = doc.getElementsByTagName("className");
            if (args.equals("road"))
            {
                classNode = nl.item(0).getFirstChild();
            } else if (args.equals("vehicle"))
            {
                classNode = nl.item(1).getFirstChild();
            }
            String cName = classNode.getNodeValue();
            Class c = Class.forName(cName);
            Object obj = c.newInstance();
            return obj;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return null;
        }
    }
}

public class Client
{
    public static void main(String[] args)
    {
        //从xml配置文件动态获取汽车类型
        Vehicle vehicle = (Vehicle) XMLUtil.getBean("vehicle");
        //动态获取路的类型
        Road road = (Road) XMLUtil.getBean("road");
        //给汽车设置行驶的路
        vehicle.setRoad(road);
        //调用汽车的行驶方法
        vehicle.drive();
    }
}

config9.xml

XML 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<config>
    <className>Tutorial_9.ConcreteRoad</className>
    <className>Tutorial_9.Bus</className>
</config>

运行效果

相关推荐
geovindu1 小时前
rust: Abstract Factory pattern
开发语言·后端·设计模式·rust·抽象工厂模式
Carl_奕然9 小时前
【智能体】Loop 的四种设计模式之:Event-Driven Loop(2026 最新版)
人工智能·驱动开发·python·设计模式
Carl_奕然10 小时前
【智能体】Loop 的四种设计模式之:Hill Climbing Loop(2026 最新版)
人工智能·python·设计模式
晚安code1 天前
设计模式入门:吃透 SOLID 原则与迪米特法则,再学 5 个高频模式
后端·设计模式
染指11101 天前
135.Agent-多Agent框架-LangChain多智能体(SubAgents子代理)
数据库·人工智能·设计模式·langchain·agent·agents
路弥行至2 天前
【Head First 设计模式】第 2 章:观察者模式 —— 用现代 C++ 重新解读“交互对象的松耦合
c++·经验分享·笔记·观察者模式·设计模式·入门教程·headfirst
YYYing.2 天前
【设计模式系列 (九) 】装饰器模式
c++·后端·设计模式·装饰器模式·c/c++
沫璃染墨2 天前
《从零入门Linux系统篇(五十六):线程篇·九——生产者消费者模型:从条件变量到BlockingQueue实现》
linux·服务器·c++·驱动开发·设计模式·架构·系统架构
c萱3 天前
AI产品经理——01基础认知篇
设计模式·prompt·aigc·产品经理·ai编程·gitcode·ai-native
YYYing.3 天前
【设计模式系列 (八) 】组合模式
c++·后端·设计模式·组合模式·c/c++