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

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

代码如下:

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 !

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值