Interface for loose coupling

Source: Internet
Author: User

9.3 Complete decoupling from Java programming ideas

As long as a method operation is a class rather than an interface, then you can only use this class and its subclasses. If you want to apply this method to a class that is not in this inheritance structure, then you cannot achieve the purpose. Interfaces can greatly relax this limitation, so using interfaces rather than inheritance allows us to write code that is more reusable. PS: The book in front also said, carefully use inheritance, try to use the combination!

For example, suppose you have a processor class that produces a name () method, and a process () method that takes an input parameter and modifies its value to produce input. This class is extended as a base class to create different processor.

Package Interfaces.classprocessor;import static Net.mindview.util.print.*;import Java.util.arrays;class Processor{ Public String name () {return getclass (). Getsimplename ();} Object process (object input) {return input;}} Class Upcase extends processor{string process (Object input) {return (String) input). toLowerCase ();}} Class Downcase extends processor{string process (Object input) {return (String) input). toUpperCase ();}} Class Splitter extends processor{string process (Object input) {return arrays.tostring ((String) input). Split (""));}} public class Apply {public static void process (Processor p,object s) {print ("Using Processor" +p.name ());p rint (P.process ( s));} public static String s = "Disagreement with beliefs are by definition incorrct";p ublic static void Main (string[] args) {Pro Cess (New Upcase (), s);p rocess (New Downcase (), s);p rocess (New Splitter (), s);}}

Output:

Using Processor Upcase
Disagreement with beliefs are by definition incorrct
Using Processor Downcase
Disagreement with beliefs are by DEFINITION INCORRCT
Using Processor Splitter
[Disagreement, with, beliefs, was, by, definition, INCORRCT]

The Apply.process () method can accept any type of processor and apply it to a single Object object, and then print the result. As we have just begun to see, we think the design is good, and create a method that behaves differently depending on the parameter object being passed, called the policy pattern. Such methods include the invariant parts of the execution algorithm, and the "strategy" contains the changed parts, and the strategy is the parameters passed in.

Now there is an electronic filter that seems to be using the Apply.process () method.

Create the relevant code in the Interfaces.filters package

Package Interfaces.filters;public class Waveform {private static long counter;private static long id = counter++;p ublic St Ring toString () {return "waveform" + ID;}}

Base class Filter Code implementation

Package Interfaces.filters;public class Filter {public String name () {return getclass (). Getsimplename ();} Public waveform process (waveform input) {return input;}}

Inherit classes generated by the filter class are inherited below

Package Interfaces.filters;public class Lowpass extends Filter {double cutoff;public lowpass (double cutoff) {This.cutoff = cutoff;} Public waveform process (waveform input) {return input;}}

Package Interfaces.filters;public class Highpass extends Filter {double cutoff;public highpass (double cutoff) { This.cutoff = cutoff;} Public waveform process (waveform input) {return input;}}

Package Interfaces.filters;public class Bandpass extends Filter {double lowcutoff,highcutoff;public bandpass (double Lowcutoff,double highcutoff) {This.lowcutoff = Lowcutoff;this.highcutoff = Highcutoff;} Public waveform process (waveform input) {return input;}}

With the above code we can see that the filter and processor have the same interface elements, but because it is not inheriting processor, you cannot apply the Apply.process () method to the filter.

We find the reason in turn, mainly because the Apply.process () method and the processor between the coupling is too tight, has exceeded the required level, so that should be reused apply.process () when reused but not used.

If we define processor as an interface, then the coupling will be reduced, the apply.process () can be reused, and the modified version is the following.

Package Interfaces.interfaceprocessor;public interface Processor {String name (); Object process (object input);}

Package Interfaces.interfaceprocessor;import static net.mindview.util.print.*;p Ublic class Apply {public static void Process (Processor p,object s) {print ("Using Processor" +p.name ());p rint (p.process (s));}}

Package Interfaces.interfaceprocessor;import Java.util.arrays;public Abstract class Stringprocessor implements Processor{public static void Main (string[] args) {apply.process (New Upcase (), String); Apply.process (New Downcase (), string); Apply.process (New Splitter (), string);} Public String name () {return getclass (). Getsimplename ();} public abstract string process (Object input);p ublic static string string = "disagreement with beliefs are by definition in CORRCT ";} 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{@Overridepublic String process (Object input) {return arrays.tostring ((String) Input). Split (""));}}
Result input:

Using Processor Upcase
Disagreement with beliefs are by DEFINITION INCORRCT
Using Processor Downcase
Disagreement with beliefs are by definition incorrct
Using Processor Splitter
[Disagreement, with, beliefs, was, by, definition, INCORRCT]

At this point we still can't apply.process the () method on the filter, but we can introduce the "adapter" design pattern. The code in the adapter will accept the interface you have and produce the interface you need, just like this:

Package Interfaces.interfaceprocessor;import Interfaces.filters.filter;import Interfaces.filters.waveform;public Class Filteradapter implements Processor {@Overridepublic String name () {return filter.name ();} @Overridepublic Waveform process (Object input) {return filter.process ((waveform) input);} Filter filter;p ublic Filteradapter (filter filter) {This.filter = filter;}}

Package Interfaces.interfaceprocessor;import Interfaces.filters.bandpass;import Interfaces.filters.HighPass; Import Interfaces.filters.lowpass;import Interfaces.filters.waveform;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);}}

Operation Result:

Using Processor lowpass
Waveform 0
Using Processor Highpass
Waveform 0
Using Processor Bandpass
Waveform 0

Interface for loose coupling

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.