mardi 27 octobre 2015

usage of Chain of Responsibility

I want to use the pattern Chain of Responsibility to check if a location (Latitude and Longitude) is on a distance. For this purpose I got a boolean which should be true if location is on distance and false if not. Before the pattern - my code looked like this:

    public static boolean OnDistance(MyLocation a, MyLocation b, MyLocation queryPoint) {
    // wenn x von a kleiner ist als b´s
    if (a.mLongitude < b.mLongitude) {
        // nd query dazwischen liegt
        if (b.mLongitude >= queryPoint.mLongitude && queryPoint.mLongitude >= a.mLongitude) {

            if (a.mLatitude > b.mLatitude) {
                if (queryPoint.mLatitude <= a.mLatitude && queryPoint.mLatitude >= b.mLatitude) {
                    System.out.println("ja!");
                    return true;
                }
            }
            if (a.mLatitude < b.mLatitude) {
                if (queryPoint.mLatitude >= a.mLatitude && queryPoint.mLatitude <= b.mLatitude) {

                    System.out.println("ja!");
                    return true;
                }
            }
        }
        {
        }

    }
    // wenn x von b kleiner ist als a´s
    if (b.mLongitude < a.mLongitude) {

        if (queryPoint.mLongitude >= b.mLongitude && queryPoint.mLongitude <= a.mLongitude) {

            if (a.mLatitude > b.mLatitude) {

                if (queryPoint.mLatitude <= a.mLatitude && queryPoint.mLatitude >= b.mLatitude) {
                    System.out.println("ja!");
                    return true;
                }
            }
            if (a.mLatitude < b.mLatitude) {
                if (queryPoint.mLatitude <= b.mLatitude && queryPoint.mLatitude >= a.mLatitude) {
                    System.out.println("ja!");
                    return true;
                }
            }
        }
    }

    System.out.println("leider nichts geworden! . lauf weiter!!!!");
    return false;
}

part of my Chain of Responsibility code (I got four classes like this one):

    public boolean check(MyLocation a, MyLocation b, MyLocation queryPoint) {
    // TODO Auto-generated method stub
    if ((a.mLongitude < b.mLongitude)
            && (b.mLongitude >= queryPoint.mLongitude && queryPoint.mLongitude >= a.mLongitude)
            && (a.mLatitude > b.mLatitude)
            && (queryPoint.mLatitude <= a.mLatitude && queryPoint.mLatitude >= b.mLatitude)) {

        System.out.println("ja, gerne!");
        onDistance = true;
        return true;

    }

    else {
        onDistance = false;
        System.out.println("weiter, lauf doch weiter");
        nextInChain.check(a, b, queryPoint);
    }

    return false;

}}

the code works but because of the "boolean return" it walks through the chain and after that it walks the complete way back to execute this returns.

My Question: First of all - is Chain of Responsibility a suitable way to "replace" this if-statments. And is there a way to stop the chain go back all the way to execute all the returns or rather what can I use instead of this boolean?

Thanks!

Aucun commentaire:

Enregistrer un commentaire