您的位置:首页 > 其它

设计模式之策略模式(Strategy Model)

2013-10-15 09:22 477 查看
设置一个接口,所有实现类实现该接口,大家传入的方法参数是一样的,但是每个实现类实现的运算是不同的,同时,用一个上下文控制类来操纵具体执行哪个具体类,这里也采用了抽象工厂模式。

代码如下:

package StrategyMethod;

public interface Strategy {
public void drawText(String s, int lineWidth, int lineCount);

}

package StrategyMethod;

public class StrategyA implements Strategy {

    public StrategyA() {

    }

    public void drawText(String text, int lineWidth, int lineCount) {

        if(lineWidth > 0) {

            int len = text.length();

            int start = 0;

            System.out.println("----- String length is :" + len + ", line width is :" + lineWidth);

            while(len > 0) {

                if(len <= lineWidth) {

                    System.out.println(text.substring(start));

                } else {

                    System.out.println(text.substring(start, start+lineWidth));

                }

                len = len - lineWidth;

                start += lineWidth;

            }

        } else {

            System.out.println("line width can not < 1 !");

        }

    }

}

package StrategyMethod;

public class StrategyB implements Strategy {

    public StrategyB() {

    }

    public void drawText(String text, int lineWidth, int lineCount) {

        if(lineCount > 0) {

            int len = text.length();

            //System.out.println(Math.ceil(len/lineCount));

            lineWidth = (int)Math.ceil(len/lineCount) + 1;

            int start = 0;

            System.out.println("-----  There are " + lineCount + " Line, " + lineWidth + "char per line -----");

            while(len > 0) {

                if(len <= lineWidth) {

                    System.out.println(text.substring(start));

                } else {

                    System.out.println(text.substring(start, start+lineWidth));

                }

                len = len - lineWidth;

                start += lineWidth;

            }

        } else {

            System.out.println("line count can not < 1 !");

        }

    }

}

package StrategyMethod;

public class Context  {

    private Strategy strategy = null;

    private int lineWidth;

    private int lineCount;

    private String text;

    

    public Context() {

        lineWidth = 10;

        lineCount = 0;

    }

    public void setStrategy(Strategy s) {

        if(s != null) {

            strategy = s;

        }

    }

    public void drawText() {

        strategy.drawText(text, lineWidth, lineCount);

    }

    public void setText(String str) {

        text = str;

    }

    public void setLineWidth(int width) {

        lineWidth = width;

    }

    public void setLineCount(int count) {

        lineCount = count;

    }

    public String getText() {

        return text;

    }

}

package StrategyMethod;

public class Test {
public static void main(String[] args) {

        int lineCount = 4;

        int lineWidth = 12;

        

        Context myContext = new Context();

        StrategyA strategyA = new StrategyA();

        StrategyB strategyB = new StrategyB();

        String s = "This is a test string ! This is a test string ! This is a test string ! This is a test string ! This is a test string ! This is a test string !";

        myContext.setText(s);

        myContext.setLineWidth(lineWidth);

        myContext.setStrategy(strategyA);

        myContext.drawText();

        myContext.setLineCount(lineCount);

        myContext.setStrategy(strategyB);

        myContext.drawText();

    }

}

输出:

----- String length is :143, line width is :12

This is a te

st string ! 

This is a te

st string ! 

This is a te

st string ! 

This is a te

st string ! 

This is a te

st string ! 

This is a te

st string !

-----  There are 4 Line, 36char per line -----

This is a test string ! This is a te

st string ! This is a test string ! 

This is a test string ! This is a te

st string ! This is a test string !
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息