码迷,mamicode.com
首页 > 其他好文 > 详细

结构型模式-桥接模式

时间:2018-12-13 17:46:23      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:color   actor   att   stat   对象关联   桥接   inter   ring   变化   

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

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

  首先是人

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

  下面是运行结果

技术分享图片

 

结构型模式-桥接模式

标签:color   actor   att   stat   对象关联   桥接   inter   ring   变化   

原文地址:https://www.cnblogs.com/liunianfeiyu/p/10115225.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!