适配器设计模式
来源:互联网 发布:dhcp 默认端口 编辑:程序博客网 时间:2024/06/11 17:35
package interfaces.filter;
public class BandPass extends Filter {
double lowCutoff,highCutoff;
public BandPass(double lowCut,double highCut){
lowCutoff = lowCut;
highCutoff =highCut;
}
public Waveform process(Waveform input){return input;}
}
/////////////////////////////////////////////////////////////////////
package interfaces.filter;
public class Filter {
public String name(){
return getClass().getSimpleName();
}
public Waveform process(Waveform input){
return input;
}
}
///////////////////////////////////////////////////////////////////////
package interfaces.filter;
public class HighPass extends Filter {
double cutoff;
public HighPass(double cutoff){
this.cutoff=cutoff;
}
public Waveform process(Waveform input){
return input;
}
}
/////////////////////////////////////////////////////////////////////
package interfaces.filter;
public class LowPass extends Filter {
double cutoff;
public LowPass(double cutoff){
this.cutoff=cutoff;
}
public Waveform process(Waveform input){
return input;
}
}
/////////////////////////////////////////////////////////////////////
package interfaces.filter;
public class Waveform {
private static long counter;
private final long id = counter++;
public String toString(){
return "Waveform"+id;
}
}
/////////////////////////////////////////////////////////////////////////////
package interfaces.interfaceprocessor;
public interface Processor {
String name();
Object process(Object input);
}
//////////////////////////////////////////////////////////////////////////////
package interfaces.interfaceprocessor;
import interfaces.filter.BandPass;
import interfaces.filter.Filter;
import interfaces.filter.HighPass;
import interfaces.filter.LowPass;
import interfaces.filter.Waveform;
/**
* 适配器设计模式
* 在这种使用适配器的方式中,FilterAdapter的构造器接受你所拥有的接口Filter,然后生成
* 具有你所需要的Processor接口的对象。
* 将接口从具体实现中解耦使得接口可以应用于多种不同的具体实现,因此代码也就更具有可复用性。
* */
class FileterAdapter implements Processor {
Filter filter;
public FileterAdapter(Filter filter) {
this.filter = filter;
// TODO Auto-generated constructor stub
}
@Override
public String name() {
// TODO Auto-generated method stub
return filter.name();
}
@Override
public Waveform process(Object input) {
// TODO Auto-generated method stub
return filter.process((Waveform)input);
}
}
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 class FilterProcessor {
public static void main(String[] args){
Waveform w = new Waveform();
Apply.process(new FileterAdapter(new LowPass(1.0)), w);
Apply.process(new FileterAdapter(new HighPass(2.0)), w);
Apply.process(new FileterAdapter(new BandPass(3.0, 4.0)), w);
}
}
- 设计模式:适配器模式
- 设计模式------适配器模式
- 设计模式:适配器模式
- 设计模式---适配器模式
- 设计模式 适配器模式
- 设计模式 - 适配器模式
- 设计模式--适配器模式
- 设计模式-适配器模式
- 设计模式------适配器模式
- 设计模式---适配器模式
- 设计模式- 适配器模式
- 设计模式 - 适配器模式
- 设计模式:适配器模式
- 适配器模式--设计模式
- 设计模式 - 适配器模式
- 设计模式-适配器模式
- 设计模式---适配器模式
- 设计模式-适配器模式
- HTML5开发实战之网易微博
- 理解SVG的缩放、偏移的计算公式
- linux(八)网络配置
- Magento2开发教程
- 【SDR】USRP使用过程中出现“U”、“O”的意义
- 适配器设计模式
- C#列表控件、信息导航控件概述
- 解读EXPLAIN执行计划中的key_len
- nodejs multer上传文件
- Machine Learning Demo
- Magento2开发教程
- 虚拟机里无法识别usb设备
- Cocos2d-x实现计时器与定时器功能
- 中国大学慕课C语言第七讲作业