首页 > 代码库 > 设计模式15---桥接模式

设计模式15---桥接模式

当一个事物或者过程有很多变化时,比如开车走路,如何我们用继承的方法,类的数目会几何增长。

但是我们如何把每个变化隔离开来,也就是,车子和路况隔离开来,这样每个变化都是独立的。

public abstract class Automobile {    public static final int TYPE_CAR = 1;    public static final int TYPE_MOTORCYCLE = 2;        public abstract int Run();}

对于路来说,它影响的是不同的比率。

package com.jayfulmath.designpattern.bridge;public abstract class Road {    public static final int TYPE_CITY = 1;    public static final int TYPE_HIGHWAY = 2;    public static final int TYPE_SUBURBAN = 3;        protected String TAGNAME;    protected Automobile _mAuto;        protected double _miles;        public abstract double SpeedRate();//汽车速度比率            public Road(double _miles) {        this._miles = _miles;    }    /**     * @param _mAuto the _mAuto to set     */    public void set_mAuto(Automobile _mAuto) {        this._mAuto = _mAuto;    }    public double Run()    {        //        System.out.println("distance of "+TAGNAME+" is "+_miles+" km");        System.out.println(String.format("distance of %s is %.0f km", TAGNAME,_miles));        double speed = _mAuto.Run()*SpeedRate();        double h = _miles/speed;        System.out.println(String.format("%s spend time %.2f hours", TAGNAME,h));        return h;    }}

car:

package com.jayfulmath.designpattern.bridge;public class Car extends Automobile {        private static Car _mInstance = null;        private Car() {        super();    }        public static Car getInstance()    {        if(_mInstance == null)        {            _mInstance = new Car();        }        return _mInstance;    }        @Override    public int Run() {        // TODO Auto-generated method stub        System.out.print("Car Run 50km/h at:");        return 50;    }}

Motorcycle:

package com.jayfulmath.designpattern.bridge;public class Motorcycle extends Automobile {        private static Motorcycle _mInstance = null;        private Motorcycle() {        super();    }        public static Motorcycle getInstance()    {        if(_mInstance == null)        {            _mInstance = new Motorcycle();        }        return _mInstance;    }        @Override    public int Run() {        // TODO Auto-generated method stub        System.out.print("Motorcycle Run 30km/h at:");        return 30;    }}

 

HighWay:

package com.jayfulmath.designpattern.bridge;public class HighWay extends Road {    public HighWay(double _miles) {        super(_miles);        this.TAGNAME = "HighWay";    }    @Override    public double SpeedRate() {        // TODO Auto-generated method stub        System.out.println("In "+TAGNAME);        return 1.5;    }}
package com.jayfulmath.designpattern.bridge;public class Suburban extends Road {    public Suburban(double _miles) {        super(_miles);        this.TAGNAME = "Suburban";    }    @Override    public double SpeedRate() {        // TODO Auto-generated method stub        System.out.println("In "+TAGNAME);        return 1.0;    }}
package com.jayfulmath.designpattern.bridge;public class CityRoad extends Road {    public CityRoad(double _miles) {        super(_miles);        this.TAGNAME = "CityRoad";    }    @Override    public double SpeedRate() {        // TODO Auto-generated method stub        System.out.println("In "+TAGNAME);        return 0.75;    }}

 汽车用简单工厂模式封装下:

package com.jayfulmath.designpattern.bridge;public class AutomobileFactory {        public static Automobile CreateAutoMobile(int carType)    {        Automobile _mAuto = null;        switch(carType)        {        case Automobile.TYPE_CAR:            _mAuto = Car.getInstance();            break;        case Automobile.TYPE_MOTORCYCLE:            _mAuto = Motorcycle.getInstance();            break;            }        return _mAuto;    }}
package com.jayfulmath.designpattern.bridge;import com.jayfulmath.designpattern.main.BasicExample;/*Bridge mode:当事物会随着很多方便变化而变化,我们可以把每个变化独立开来 * 对于不同的方面,可以进行相应的 *  * */public class BridgeMain extends BasicExample {    @Override    public void startDemo() {        /*1.highway 50km car         *2.cityWay 30km motorcycle          *3.suburban 10km car          * */            double h1 = getRunTime(Road.TYPE_HIGHWAY,Automobile.TYPE_CAR,50);        double h2 = getRunTime(Road.TYPE_CITY,Automobile.TYPE_MOTORCYCLE,30);        double h3 = getRunTime(Road.TYPE_SUBURBAN,Automobile.TYPE_CAR,10);                System.out.println("Total time is "+(h1+h2+h3));            }        private double getRunTime(int roadtype,int cartype,int pathlength)    {        double hour = 0;        Road _mRoad = RoadFactory.CreateRoad(roadtype, pathlength);        _mRoad.set_mAuto(AutomobileFactory.CreateAutoMobile(cartype));        hour = _mRoad.Run();        System.out.println("***************************************");        return hour;    }}

结果:

distance of HighWay is 50 kmCar Run 50km/h at:In HighWayHighWay spend time 0.67 hours***************************************distance of CityRoad is 30 kmMotorcycle Run 30km/h at:In CityRoadCityRoad spend time 1.33 hours***************************************distance of Suburban is 10 kmCar Run 50km/h at:In SuburbanSuburban spend time 0.20 hours***************************************Total time is 2.2

实现系统有多角度分类,每个角度都有可能变化,把它们独立开来,不用继承的方式,而是用聚合的方式,可以避免类的爆炸。

设计模式15---桥接模式