• 策略与适配器设计模式


    1、策略设计模式

    import java.util.Arrays;
    
    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);
    	}
    }
    
    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(" ")));
    	}
    }
    

      输出:

    Using processor Upcase
    DISAGREEMENT WITH BELIEFS IS BY DEFINITION INCORRECT
    Using processor Downcase
    disagreement with beliefs is by definition incorrect
    Using processor Splitter
    [Disagreement, with, beliefs, is, by, definition, incorrect]

    分析:对于不同的processor对象(即策略),应用于字符串s会产生不同的行为。

    2、适配器设计模式

    import java.util.Arrays;
    
    public class Adapter {
    	public static void process(Processor p, Object s) {
    		System.out.println("Using processor " + p.name());
    		System.out.println(p.process(s));
    	}
    
    	public static void main(String[] args) {
    		String s = "abcdefg";
    		process(new ExerAdapter(new Exer()), s);
    	}
    }
    
    interface Processor {
    	String name();
    
    	Object process(Object input);
    }
    
    class Exer {
    	String func(String s) {
    		if (s != null && s != "") {
    			char[] split = s.toCharArray();
    			for (int i = 0; i < split.length - 1; i += 2) {
    				char temp = split[i];
    				split[i] = split[i + 1];
    				split[i + 1] = temp;
    			}
    			s = Arrays.toString(split);
    		}
    		return s;
    	}
    }
    
    /**
     * 适配器转换类
     */
    class ExerAdapter implements Processor {
    
    	Exer e;
    
    	public ExerAdapter(Exer e) {
    		this.e = e;
    	}
    
    	@Override
    	public String name() {
    		return e.getClass().getSimpleName();
    	}
    
    	@Override
    	public Object process(Object input) {
    		return e.func((String) input);
    	}
    
    }
    

      输出:

    Using processor Exer
    [b, a, d, c, f, e, g]

    分析:由于Exer与Process接口不一致,所以新增一个ExerAdapter适配器类来对Exer适配,应用于Adapter的process方法。

  • 相关阅读:
    闲扯 Javascript 01 实现选项卡
    控制台获得键盘事件
    C#反射 入门学习 02
    C#反射 入门学习 01
    闲扯 Javascript 00
    读张子阳老师的委托和事件 2
    浅析ado.net获取数据库元数据信息 DeriveParameters
    SQLBulkCopy使用
    利用CryptoStream进行加密解密
    vs 中代码的字体也颜色设置
  • 原文地址:https://www.cnblogs.com/lirun/p/11698351.html
Copyright © 2020-2023  润新知