现在的位置: 首页 > 综合 > 正文

设计模式-结构型模式-策略

2017年12月08日 ⁄ 综合 ⁄ 共 3128字 ⁄ 字号 评论关闭

行为模式涉及到算法和对象间职责的分配。
strategy:定义一系例的算法,把它们一个个封装起来,并且可以使他们相互替换。可以使得算法独立应用于客户的变化。

 

package behaviour.strategy;
/**
 *  The context user used
 */
import java.io.*;

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 behaviour.strategy;
/**
 *  The public interface to support varies arithmetic
 */
public interface Strategy {
    public void drawText(String s, int lineWidth, int lineCount);
}

 

package behaviour.strategy;
/**
 *  A concrete strategy to draw a text by the width of line
 */
import java.io.*;

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 behaviour.strategy;
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 behaviour.strategy;
/**
 *  A test client
 */
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();
    }
}

抱歉!评论已关闭.