RSS
 

Refactored Bonding

21 Mar

I refactored the code in XKCD’s Bonding :

public class Fish extends Throwable {

}

public class P {
    P target;

    P(P target) {
        this.target = target;
    }

    void aim(Fish fish) {
        try {
            throw fish;
        } catch (Fish f) {
            target.aim(f);
        }
    }

    public static void main(String[] args) {
        P calvin = new P(null);
        P hobbes = new P(calvin);
        calvin.target = hobbes;
        calvin.aim(new Fish());
    }
}


 
 

Tags: , , , , , , ,

Leave a Reply