Tuesday, 19 April 2016

Strategy pattern - Example


Compress files using different algorithms or save files in different formats or draw graphic in different presentations.

interface FortuneCookies {
    public void print();
}
class Five implements FortuneCookies {
    public void print() {
        System.out.println("It is your turn to get it");
    }
}
class Two implements FortuneCookies {
    public void print() {
        System.out.println("It is never too late to start");
    }
}
class Null implements FortuneCookies {
    public void print() {
        System.out.println("You got nothing");   
    }
}
class Dice {
   public int throwIt() {
       return (int)(Math.random()*6)+1;
   }
}

// more class...
class Test { 
    static void goodFortune() {
        int luckyNum = new Dice().throwIt();
        FortuneCookies fc;
        switch (luckyNum) {
            case 2: fc = new Two();
                    break;
            case 5: fc = new Five();
                    break;
            // more
            default: fc = new Null();
        }
        fc.print();
    }
    public static void main(String[] args) {
        goodFortune();
    }
}

No comments:

Post a Comment

Note: only a member of this blog may post a comment.