结构型模式-桥接模式

Posted liunianfeiyu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了结构型模式-桥接模式相关的知识,希望对你有一定的参考价值。

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

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

  首先是人

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();
    }
}

  下面是运行结果

技术分享图片

 

以上是关于结构型模式-桥接模式的主要内容,如果未能解决你的问题,请参考以下文章

[设计模式C++go]结构型模式:桥接模式

[设计模式C++go]结构型模式:桥接模式

Python 设计模式 — 结构型模式 — 桥接模式

Python 设计模式 — 结构型模式 — 桥接模式

设计模式之桥接模式

设计模式之桥接模式(结构型)