Created
May 25, 2012 04:02
-
-
Save sanpingz/2785677 to your computer and use it in GitHub Desktop.
适配器模式
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static com.mceiba.util.Print.*; | |
import java.util.*; | |
class Filter{ | |
public String name(){ | |
return getClass().getSimpleName(); | |
} | |
public Wavaform process(Wavaform input) { return input; } | |
} | |
class FilterAdapter implements Processor{ | |
Filter filter; | |
public FilterAdapter(Filter filter) { this.filter = filter; } | |
public String name(){ return filter.name(); } | |
public Wavaform process(Object input){ | |
return filter.process((Wavaform)input); | |
} | |
} | |
public class Adapter{ | |
public static void main(String[] args){ | |
Strategy.process(new FilterAdapter(new LowPass(1.0)), new Wavaform()); | |
Strategy.process(new FilterAdapter(new HighPass(2.0)), new Wavaform()); | |
Strategy.process(new FilterAdapter(new BandPass(3.0, 4.0)), new Wavaform()); | |
} | |
} | |
class Wavaform{ | |
private static long counter; | |
private final long id = counter++; | |
public String toString() { return "Wavaform "+id; } | |
} | |
class LowPass extends Filter{ | |
double cutoff; | |
public LowPass(double cutoff) { this.cutoff = cutoff; } | |
public Wavaform process(Wavaform input) { return input; } | |
} | |
class HighPass extends Filter{ | |
double cutoff; | |
public HighPass(double cutoff) { this.cutoff = cutoff; } | |
public Wavaform process(Wavaform input) { return input; } | |
} | |
class BandPass extends Filter{ | |
double lowCutoff, highCutoff; | |
public BandPass(double lowCutoff, double highCutoff){ | |
this.lowCutoff = lowCutoff; | |
this.highCutoff = highCutoff; | |
} | |
public Wavaform process(Wavaform input) { return input; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment