/*** Book: Thinking in Java * Features: Adapter design mode * File: filteradapter.java* time: April 2, 2015 20:52:07* Author: cutter_point*/package Lesson9interfaces.interfaceprocessor;import Lesson9interfaces.filters.bandpass;import Lesson9interfaces.filters.filter;import Lesson9interfaces.filters.highpass;import Lesson9interfaces.filters.lowpass;import Lesson9interfaces.filters.waveform;class FilterAdapter Implements Processor//This class, feeling is a transit point, bar a filter type parameter passed in, converted to the Filteradapter type, and this type is actually exported from Processor, then the filter type and the Processor type are combined { Filter Filter;public Filteradapter (filter filter) {This.filter = filter;} @Overridepublic String name () {return filter.name ();} @Overridepublic Waveform process (Object input) {return filter.process ((waveform) input);}} public class Filterprocessor{public static void Main (String [] args) {Waveform w = new waveform (); Apply.process (New Filteradapter (New Lowpass (1.0)), W); Apply.process (New Filteradapter (New Highpass (2.0)), W); Apply.process (New Filteradapter (New Bandpass (3.0, 4.0)), w);}}
Using adapter design mode, the code in the adapter will accept the interface you have and produce the interface you need
I think, is the combination of classes, the original class encapsulated into the new class, in the new class to implement the requirements of the various interfaces
Output:
Using Processor lowpass obj1
Waveform 0 Obj1
Using Processor Highpass obj1
Waveform 0 Obj1
Using Processor bandpass obj1
Waveform 0 Obj1
"Thinkinginjava" 14, Adapter design mode (2)