"Thinkinginjava" 14, Adapter design mode (2)

Source: Internet
Author: User

/*** 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.