[Thiinking in Java] autistic way of learning (c) at the interface

Foreword -

This article is to learn to sort out the nature of the personal notes do, certainly with a lot wrong with it, for information purposes only.

(Chapter IX · Interface "Thinking in Java")

  The interface is not just a more pure form of abstract class

 

 

text-

  The interface is not just a more pure form of abstract class, his goal is even higher than that. For example, " complete decoupling " and " multiple inheritance "

 

  The first is completely decoupled , and that you want to have some understanding of the coupling, as long as a method of operation of the class rather than an interface, then you can only use this class and its subclasses.

  Processor Now we have a class that contains the name () method and process () method. Splitter and overrides methods inherited Processor process () method, implement the functions according to the empty string Geqie division.

  In the Apply, Apply.process () method may accept any type of Processor (as long as it is inherited from Processor), and apply it to the object is an Object, and then print the results.

class Processor {
    public String name() {
        return getClass().getSimpleName();
    }
    Object process(Object input) {
        return input;
    }
}

class Splitter extends Processor{
    @Override
    Object process(Object input) {
        // the TODO automatically generated stub method 
        return of Arrays.toString (((String) INPUT) .split ( "" ));
    }
}

public 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="i love you baby";
    public static void main(String[] args) {
        process(new Splitter(),s);
    }
}
Inheritance call

  ※ like example of such, according to the method can create a different behavior but with different parameter object preaching, called the strategy design pattern . Such methods include algorithms to be executed in the fixed part, and the "Strategy" section contains the changes.

  ※ strategy is to pass parameters into the object, which contains the code to be executed. Here Processor is a strategy.

  But these are not the focus, Splitter implementation is not the focus.

  Remember Processor look! It is the key!

Next, another set of classes

public class Waveform {//    波形类
    private static long count;
    private final  long id=count++;
    public String toString(){
        return "Waveform "+id;
    }
}

class the Filter { //     adapter class 
    public String name () {
         return getClass () getSimpleName ().;
    }
    Object process(Waveform input) {
        return input;
    }
}

class BANDPASS the extends the Filter { // filter type 
    Double   lowCutoff, hightCutoff;
     public BANDPASS ( Double   lowCutoff, Double hightCutoff) {
         // the TODO automatically generated constructor stub 
        the this .lowCutoff = lowCutoff;
         the this .hightCutoff = hightCutoff;
    }
    @Override
        Object process(Waveform input) {
            // the TODO automatically generated stub method 
            return INPUT;
        }
}
Another set of classes

  Observation of Filter and Processor, her two elements has the same interface. If you try to call static of Apply.process (p, s) in the mian method;, the compiler will prompt Apply the process (p, s) does not apply. Because you're not in this method is applied to an inheritance structure in a class.

  ※ When tight coupling between Apply.process () on the Processor.

Use of this interface can be largely relaxed couple, now we will change the interface type Processor

public interface iProcessor {
    String name();
    Object process(Object input);
}
public  class StringProcessor the implements iProcessor {
     public  static String S = "I Love you Baby" ;
     public  static  void main (String [] args) {
         // method TODO automatically generated stubs 
        NewApply.process ( new new Splitter (), S);
    }

    @Override
    public String name () {
         // method TODO automatically generated stubs 
        return getClass () getSimpleName ().;
    }

    @Override
    public Object Process (Object INPUT) {
         // the TODO automatically generated stub method 
        return INPUT;
    }

}
class NewApply{
    public static void process(Processor p,Object s) {
        System.out.println("Using Processor "+p.name());
        System.out.println(p.process(s));
    }
}

class NewSplitter extends StringProcessor{
    @Override
    public Object Process (Object INPUT) {
         // the TODO automatically generated stub method 
        return of Arrays.toString (((String) INPUT) .split ( "" ));
    }
}
View Code

 

Now Processor is an interface into iProcessor, how it applies to Wavefform?

You can use the adapter design pattern . Accepted Interface (type) you have an adapter through Adapter, and generate the interface (type) you need a.

public class FilterAdapter implements iProcessor {
    Filter f;
    public FilterAdapter (the Filter F) {
         // the TODO automatically generated constructor stub 
        the this .F = F;     //     will do just passed in the fiter adapted 
    }
    @Override
    public String name () {
         // method TODO automatically generated stubs 
        return F.Name ();
    }

    @Override
    public Object Process (Object INPUT) {
         // the TODO automatically generated stub method 
        return f.process ((Waveform) INPUT);
    }

}
Adapter
public class FilterProcessor {

    public  static  void main (String [] args) {
         // method TODO automatically generated stubs 
        Waveform W = new new Waveform ();
        NewApply.process(new FilterAdapter(new BandPass(3.0, 4.0)), w);
    }

}
transfer

So we accept Filter Adapter type you have the original, and produced a iProcessor object interface you need.

 

※ Although the adapter looks very practical, but use her as little as possible. When you use excessive conversion adapter, you should rethink the design is not what's wrong.

Guess you like

Origin www.cnblogs.com/YFEYI/p/12178798.html