通知 网站从因情语写改为晴雨,这个网站的模板也从calmlog_ex改为 whimurmur

结构型模式-桥接模式

2598人浏览 / 0人评论 / | 作者:因情语写  | 分类: 设计模式与算法  | 标签: 设计模式与算法  | 

作者:因情语写

链接:https://www.qingyu.blue/article/444

声明:请尊重原作者的劳动,如需转载请注明出处


  要组合几个对象,这几个对象又都会变化,把几个对象关联起来,可以用桥接模式

  看例子:人,车,路,人开车在路在行驶,人,车,路都可以变化

  首先是人

package constructional.pattern.bridge;

public interface People {
    void run();
}
package constructional.pattern.bridge;

public class Man implements People {
    private Road road;
    
    public Man(Road road) {
        super();
        this.road = road;
    }

    @Override
    public void run() {
        System.out.print("man on the ");
        road.run();

    }

}
package constructional.pattern.bridge;

public class Woman implements People {
    
    private Road road;
    
    public Woman(Road road) {
        super();
        this.road = road;
    }

    @Override
    public void run() {
        System.out.print("woman on the ");
        road.run();
    }

}

  下面是路

package constructional.pattern.bridge;

public interface Road {
    void run();
}
package constructional.pattern.bridge;

public class HighSpeedRoad implements Road {
    private Vehicle vehicle;
    
    public HighSpeedRoad(Vehicle vehicle) {
        super();
        this.vehicle = vehicle;
    }


    @Override
    public void run() {
        System.out.print("highspeed road run the ");
        vehicle.run();
    }

}
package constructional.pattern.bridge;

public class Street implements Road {
    private Vehicle vehicle;
    
    public Street(Vehicle vehicle) {
        super();
        this.vehicle = vehicle;
    }

    @Override
    public void run() {
        System.out.print("street road run the ");
        vehicle.run();
    }

}

  最后是车

package constructional.pattern.bridge;

public interface Vehicle {
    void run();
}
package constructional.pattern.bridge;

public class Car implements Vehicle {

    @Override
    public void run() {
        System.out.println("car.");
    }

}
package constructional.pattern.bridge;

public class Bus implements Vehicle {

    @Override
    public void run() {
        System.out.println("bus.");
    }

}

  如何组合

package constructional.pattern.bridge;

public interface RunFactory {
    int MAN = 1;
    int WOMAN = 0;
    int STREET = 1;
    int HIGHSPEEDROAD = 2;
    int BUS = 1;
    int CAR = 2;
    
    People getRun(int people, int road, int vehicle);
    
    Road getRun(int road, int vehicle);
    
    Vehicle getRun(int vehicle);
}
package constructional.pattern.bridge;

public class RunFactoryImp implements RunFactory {

    @Override
    public People getRun(int people, int road, int vehicle) {
        if(people == RunFactory.MAN)
        {
            return new Man(getRun(road, vehicle));
        }
        else if(people == RunFactory.WOMAN)
        {
            return new Woman(getRun(road, vehicle));
        }
        
        return null;
    }

    @Override
    public Road getRun(int road, int vehicle) {
        if(road == RunFactory.HIGHSPEEDROAD)
        {
            return new HighSpeedRoad(getRun(vehicle));
        }
        else if(road == RunFactory.STREET)
        {
            return new Street(getRun(vehicle));
        }
        
        return null;
    }

    @Override
    public Vehicle getRun(int vehicle) {
        if(vehicle == RunFactory.BUS)
        {
            return new Bus();
        }
        else if(vehicle == RunFactory.CAR)
        {
            return new Car();
        }
        
        return null;
    }

}

  看一下测试代码

package constructional.pattern.bridge;

public class Client {
    public static void main(String[] args) {
        int v1 = 1;
        int v2 = 2;
        int r1 = 1;
        int r2 = 2;
        int p0 = 0;
        int p1 = 1;
        
        RunFactory runFactory = new RunFactoryImp();
        People pp1 = runFactory.getRun(p1, r1, v1);
        pp1.run();
        
        People pp2 = runFactory.getRun(p1, r1, v2);
        pp2.run();
        
        People pp3 = runFactory.getRun(p0, r2, v1);
        pp3.run();
    }
}

  下面是运行结果


亲爱的读者:有时间可以点赞评论一下

点赞(0) 打赏

全部评论

还没有评论!