美文网首页
设计模式-策略模式(十二)

设计模式-策略模式(十二)

作者: 巨子联盟 | 来源:发表于2018-05-24 20:02 被阅读0次

    策略模式,又叫政策模式

    • 用意: 针对一组算法,抽象出一个接口,每个算法实现这个接口,使得客户端可以相互替换,关键点是算法替换

    动态的让一个对象从许多的行为中选择一种实现.

    使用的太频繁了.
    类图:


    策略模式.png

    示例代码:

    1. 先创建一个接口,需要实现的算法
    package com.byedbl.strategy;
    
    /**
     *  The public interface to support varies arithmetic
     */
    public interface Strategy {
        public void drawText(String s, int lineWidth, int lineCount);
    }
    
    1. 再分别对这个算法搞两种不同的实现
    package com.byedbl.strategy; /**
     *  A concrete strategy to draw a text by the width of line
     */
    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 com.byedbl.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 !");
            }
        }
    }
    
    1. 有个Context类对引用.其实没有的话就客户端直接调也可以
    package com.byedbl.strategy; /**
     *  The context user used
     */
    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;
        }
    }
    
    1. 客户端用法
    package com.byedbl.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();
        }
    }
    

    换一种策略就一种实现了...很实用,,,很经典,,,很牛逼....

    相关文章

      网友评论

          本文标题:设计模式-策略模式(十二)

          本文链接:https://www.haomeiwen.com/subject/qfnsjftx.html