• 桥接模式(Bridge)_java实现


    //20210120
    写在前面:刚期末考试完,考了面向对象,里边儿有23个设计模式,我寻思着考完挨个儿实现一下,本文实现桥接模式

    桥接模式核心思想

    • 实现多对多模式,比如很多品牌的汽车要跑很多种类的路,汽车的品牌和路的种类是多对多的关系,如果一一映射实现的话,会非常浪费资源,不如创建一个虚拟的桥的概念,让配对的车和路在桥上相遇,这样就实现了类的种类数 = 车品牌数 + 路的种类数,而不用穷举每一种映射,而且使用桥接模式也很符合开闭原则,不用动抽象类,直接增加扩展包即可
    • 理解如图:

    程序源代码如下:

    //车辆抽象类
    public abstract class Car {
        protected Road road;
    
        protected String name;
    
        //设置手机软件
        public void setRoad(Road road){
            this.road = road;
        }
    
        //运行
        public abstract void run();
        public abstract String getName();
    }
    
    //吉普车实现类
    public class Jeep extends Car {
    
        public Jeep(){
            this.name = "吉普车";
        }
    
        public String getName() {
            return name;
        }
    
        @Override
        public void run() {
            road.run();
        }
    }
    
    //公交车实现类
    public class Bus extends Car{
    
        public Bus(){
            this.name = "公交车";
        }
    
        public String getName() {
            return name;
        }
    
        @Override
        public void run() {
            road.run();
        }
    }
    
    //皮卡实现类
    public class PickUpTruck extends Car {
    
        public PickUpTruck(){
            this.name = "皮卡";
        }
    
        public String getName() {
            return name;
        }
    
        @Override
        public void run() {
            road.run();
        }
    }
    
    
    //道路抽象类
    public abstract class Road {
        public abstract void run();
    }
    
    
    //沙路实现类
    public class SandRoad extends Road {
        @Override
        public void run() {
            System.out.println("在沙子路上跑");
        }
    }
    
    
    //石头路实现类
    public class StoneRoad extends Road {
        @Override
        public void run() {
            System.out.println("在石头路上跑");
        }
    }
    
    
    //公路实现类
    public class HighWayRoad extends Road{
        @Override
        public void run() {
            System.out.println("在公路上跑");
        }
    }
    
    //测试主类
    public class Main {
        public static void main(String[] args) {
            Car jeep;
            jeep = new Jeep();
            System.out.println(jeep.getName());
            jeep.setRoad(new StoneRoad());
            System.out.print(jeep.getName()+": ");
            jeep.run();
            jeep.setRoad(new SandRoad());
            System.out.print(jeep.getName()+": ");
            jeep.run();
            jeep.setRoad(new HighWayRoad());
            System.out.print(jeep.getName()+": ");
            jeep.run();
    
            System.out.println("=====================");
    
            Car bus;
            bus = new Bus();
            System.out.println(bus.getName());
            bus.setRoad(new HighWayRoad());
            System.out.print(bus.getName()+": ");
            bus.run();
    
            System.out.println("=====================");
    
            Car pickUpTruck;
            pickUpTruck = new PickUpTruck();
            System.out.println(pickUpTruck.getName());
            pickUpTruck.setRoad(new SandRoad());
            System.out.print(pickUpTruck.getName()+": ");
            pickUpTruck.run();
            pickUpTruck.setRoad(new HighWayRoad());
            System.out.print(pickUpTruck.getName()+": ");
            pickUpTruck.run();
        }
    }
    
    
    • 输出如下:

    总结:

    • 这样我们新增道路种类和车辆种类,直接新建类即可,而不用改动框架源代码

    以上
    希望对大家有所帮助

  • 相关阅读:
    使用session页面控制登录入口及购物车效果的实现
    php中会话保持 session 与cooker
    php多关键字查询
    php后台编辑关联数据
    php后台增删改跳转
    php登录注册页面及加载
    [bzoj4098] [Usaco2015 Open]Palindromic Paths
    [bzoj1969] [Ahoi2005]LANE 航线规划
    4395: [Usaco2015 dec]Switching on the Lights
    [bzoj2789] [Poi2012]Letters
  • 原文地址:https://www.cnblogs.com/lavender-pansy/p/14305449.html
Copyright © 2020-2023  润新知