【ThinkingInJava】13、适配器设计模式
来源:互联网 发布:java程序设计教程 pdf 编辑:程序博客网 时间:2024/05/17 06:48
/*** 书本:《Thinking In Java》* 功能:适配器设计模式* 文件:Processor.java* 时间:2015年4月2日20:36:59* 作者:cutter_point*/package Lesson9Interfaces.interfaceprocessor;public interface Processor {String name();Object process(Object input);}
/*** 书本:《Thinking In Java》* 功能:适配器设计模式* 文件:Apply.java* 时间:2015年4月2日20:36:59* 作者:cutter_point*/package Lesson9Interfaces.interfaceprocessor;import static net.mindview.util.Print.*;public class Apply {public static void process(Processor p, Object s){print("Using Processor " + p.name());print(p.process(s));}}
/*** 书本:《Thinking In Java》* 功能:适配器设计模式* 文件:StringProcessor.java* 时间:2015年4月2日20:36:59* 作者:cutter_point*/package Lesson9Interfaces.interfaceprocessor;import java.util.Arrays;public abstract class StringProcessor implements Processor {@Overridepublic String name() {return this.getClass().getSimpleName();}@Overridepublic abstract String process(Object input);public static String s = "If she weights the same as a duck, she`s made of wood";public static void main(String [] args){Apply.process(new Upcase(), s);Apply.process(new Downcase(), s);Apply.process(new Splitter(), s);}}class Upcase extends StringProcessor{public String process(Object input) {return ((String) input).toUpperCase();}}class Downcase extends StringProcessor{public String process(Object input) {return ((String) input).toLowerCase();}}class Splitter extends StringProcessor{public String process(Object input) {return Arrays.toString( ( (String) input).split(" "));}}
上一篇里面说过,那两个类耦合太高了,我们如何解耦就在这篇里面,我们可以吧Processor类写成接口,这样用一个抽象类实现这个接口,那么耦合就会变得松动,那么Apply.process()就可以得到复用,接收一个Object参数!!!
输出结果:
Using Processor Upcase obj1
IF SHE WEIGHTS THE SAME AS A DUCK, SHE`S MADE OF WOOD obj1
Using Processor Downcase obj1
if she weights the same as a duck, she`s made of wood obj1
Using Processor Splitter obj1
[If, she, weights, the, same, as, a, duck,, she`s, made, of, wood] obj1
0 0
- 【ThinkingInJava】13、适配器设计模式
- 【ThinkingInJava】14、适配器设计模式 (2)
- 【ThinkingInJava】42、装饰器设计模式
- 设计模式利剑13-适配器模式
- 设计模式利剑13-适配器模式
- 设计模式:适配器模式
- 设计模式------适配器模式
- 设计模式:适配器模式
- 设计模式---适配器模式
- 设计模式 适配器模式
- 设计模式 - 适配器模式
- 设计模式--适配器模式
- 设计模式-适配器模式
- 设计模式------适配器模式
- 设计模式---适配器模式
- 设计模式- 适配器模式
- 设计模式 - 适配器模式
- 设计模式:适配器模式
- Do Lots of Deliberate Practice
- 需要看看printf源码的一道面试题目
- <PY>Python扩展包安装方法(待更新)
- Domain-Specific Languages
- Don’t Be Afraid to Break Things
- 【ThinkingInJava】13、适配器设计模式
- 【OI再见】
- java 项目 获取当前路径
- Bitwise AND of Numbers Range
- [Leetcode 101, Easy] Symmetric Tree
- 利用iText生成pdf报表
- springMVC小问题
- [Leetcode 139, medium] Word Break
- 数据结构知识总结