设计图转架构

2017-07-06  本文已影响12人  王小贱_ww
//TripManager 应该写成一个单例模式
public class TripManager {
//在TripManager中,我们应该启动GPS和Sensor,不能把这两个类放到这里,因为当这两个类有改动的时候,还需要再次修改TripManager 类,这里把这两个类在包一层TripTracker
    private TripTracker mTracker;


    public void startTrip() {
        mTracker = new TripTracker();
        mTracker.startTracker();
    }


    public void stopTrip() {
        mTracker.stopTracker();
    }
}
public class TripTracker implements GpsTracker.onGpsChangedListener, SensorTracker.onSensorChangedListener {
    private GpsTracker gpsTracker;
    private SensorTracker sensorTracker;
    private TripWriter mtripWriter;

    public void startTracker() {
        gpsTracker = new GpsTracker();
        gpsTracker.startLocation(this);
        sensorTracker = new SensorTracker();
        sensorTracker.startSensor(this);

        mtripWriter = new TripWriter();
    }

    public void stopTracker() {
        gpsTracker.stopLocation();
        sensorTracker.stopSensor();

        mtripWriter.close();
    }

    @Override
    public void onGpsChanged(GpsTracker.GpsEntity gpsEntity) {
        mtripWriter.onGpschanged(gpsEntity);
    }

    @Override
    public void onSensorChanged(SensorTracker.SensorEntity sensorEntity) {
        mtripWriter.onSensorchanged(sensorEntity);
    }
}
public class GpsTracker {
  //接口回调
    private onGpsChangedListener listener;

    public void startLocation(onGpsChangedListener listener) {
        this.listener = listener;
    }

    public void stopLocation() {

    }
//回调的时候,如果写成(String a,String b。。。)如果写成这样,当返回参数需要修改时,回调的地方也需要修改,所以这里写成一个GpsEntity。
    public static class GpsEntity {
        public int timestamp;
        public long lat;
        public long lan;

    }

    interface onGpsChangedListener {
        void onGpsChanged(GpsEntity gpsEntity);
    }
}
public class SensorTracker {
    private onSensorChangedListener listener;

    public void startSensor(TripTracker listener) {
        this.listener = listener;
    }

    public void stopSensor() {

    }

    public static class SensorEntity {

        public int timestamp;
        public float[] values;
    }

    interface onSensorChangedListener {
        void onSensorChanged(SensorEntity sensorEntity);
    }
}
public class TripWriter {
    public void onGpschanged(GpsTracker.GpsEntity gpsEntity) {

    }

    public void onSensorchanged(SensorTracker.SensorEntity sensorEntity) {

    }

    public void close() {

    }
}
上一篇 下一篇

猜你喜欢

热点阅读