庄泽波の博客

好记性不如烂笔头

Pattern Design - Strategy Pattern

   If you can disassemble code  and  you want to apply one method of an object

in another object that there is no inheritance between them, you can use Strategy

Pattern. Aha, actually it is good strategy that we can wrap different kinds of algorithms,

then pass any of them to solve problem in different kinds of situation. 

  Now let's dive into it and show how it works!

 

1. Duck Class

package Duck;

/**
 * Created by zhuangzebo on 2017/6/30.
 * Duck.java
 */

public abstract class Duck {
    FlyBehavior flyBehavior;
    QuackBehavior quackBehavior;

    public Duck() {

    }

    public abstract void display();
    public void performFly() {
        flyBehavior.fly();
    }
    public void performQuack() {
        quackBehavior.quack();
    }

    public void swim() {
        System.out.println("All ducks float, even decoys");
    }
    public void setFlyBehavior(FlyBehavior fb) {
        flyBehavior = fb;
    }
    public void setQuackBehavior(QuackBehavior qb) {
        quackBehavior = qb;
    }
}

  

package Duck;

/**
 * Created by zhuangzebo on 2017/6/30.
 * ModelDuck.java
 */
public class ModelDuck extends Duck {
    public ModelDuck() {
        flyBehavior = new FlyNoWay();  // Can not fly at first.
        quackBehavior = new Quack();
    }
    public void display() {
        System.out.println("I'm flying with a rocket.");
    }
}

  

2.FlyBehavior Class

package Duck;

/**
 * Created by zhuangzebo on 2017/6/30.
 * FlyBehavior.java
 */

/**
 * We declare a flying behavior interface so that
 * we can implement different kinds of behaviors
 * to ducks.
 */

public interface FlyBehavior {
    public void fly();
}

 

package Duck;

/**
 * Created by zhuangzebo on 2017/6/30.
 * FlyNoWay.java
 */
public class FlyNoWay implements FlyBehavior {
    public void fly() {
        System.out.println("I can't fly.");
    }
}

  

package Duck;

/**
 * Created by zhuangzebo on 2017/6/30.
 */

/**
 * FlyWithWings
 * FlyWithWings.java
 */

public class FlyWithWings implements FlyBehavior {
    public void fly() {
        System.out.println("I'm flying");
    }
}

 

3.QuackBehavior Class

package Duck;

/**
 * Created by zhuangzebo on 2017/6/30.
 * QuackBehavior.java
 */
public interface QuackBehavior {
    public void quack();
}

  

package Duck;

/**
 * Created by zhuangzebo on 2017/6/30.
 * Quack.java
 */
public class Quack implements QuackBehavior {
    public void quack() {
        System.out.println("Quack");
    }
}

 

package Duck;

/**
 * Created by zhuangzebo on 2017/6/30.
 * MuteQuack.java
 */
public class MuteQuack implements QuackBehavior {
    public void quack() {
        System.out.println("<< Silence >>");
    }
}

  

4. Test.

package Duck;

/**
 * Created by zhuangzebo on 2017/6/30.
 * MultiDuckSimulator.java
 */
public class MultiDuckSimulator {
    public static void main(String[] args) {
        Duck model = new ModelDuck();
        model.performFly();
        model.setFlyBehavior(new FlyWithWings());
        model.performFly();

        model.performQuack();
        model.setQuackBehavior(new MuteQuack());
        model.performQuack();
    }
}

 

I can't fly.
I'm flying
Quack
<< Silence >>

  

From the code above, We can know the apperance of strategy pattern. Here we can learn more for another example.

package processor;

/**
 * Created by zhuangzebo on 2017/6/30.
 * Apply.java
 */

import java.util.*;

class Processor {
    public String name() {
        return getClass().getSimpleName();
    }

    Object process(Object input) {
        return input;
    }
}

class Upcase extends Processor {
    String process(Object input) {
        return ((String)input).toUpperCase();
    }
}

class DownCase extends Processor {
    String process(Object input) {
        return ((String)input).toLowerCase();
    }
}


class Splitter extends Processor {
    String process(Object input) {
        return Arrays.toString(((String)input).split(" "));
    }
}

public class Apply {
    public static void process(Processor p, Object s) {
        System.out.println("Using Processor " + p.name());
        System.out.println(p.process(s));
    }

    public static String s = "Disagreement with beliefs is by definition incorrect.";
    public static void main(String[] args) {
        process(new Upcase(), s);
        process(new DownCase(), s);
        process(new Splitter(), s);
    }
}

  

 

posted on 2017-06-30 23:23  庄泽波  阅读(144)  评论(0编辑  收藏  举报

导航