Java fully decoupled [9.4 of programming ideas]

Source: Internet
Author: User
Tags object object

Policy Design Patterns

Example

Package COM.EBAO.JAVA.INTERFACES9;

public class Processor {
Public String name () {
Return This.getclass (). Getsimplename ();
}
Object process (object input) {
return input;
}
}

public class Upcase extends Processor {
String process (Object input) {
return (String) input. toUpperCase ();
}
}

public class Downcase extends Processor {
String process (Object input) {
return (String) input. toLowerCase ();
}
}

public 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) {
System.out.println ("Using Processor" +p.name ());
System.out.println (p.process (s));
}
public static String s = "Disagreement with beliefs are by definition incorrect";
public static void Main (string[] args) {
Process (new Upcase (), s);
Process (new Downcase (), s);
Process (new Splitter (), s);
}
}

Operation Result:

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

The Apply.process () method can accept any type of process and apply it to a single Object object, and then print the result. Like the example above, creating a method that behaves differently depending on the parameter object being passed is called a policy design pattern. Such methods contain fixed portions of the algorithm to be executed, and "policy" contains the changed parts. A policy is a parameter object that is passed in, which contains the code to execute. Here, the Processor object is a policy, and in main () you can see that there are three different types of policies applied to the S object of type string.

Example two:

Package COM.EBAO.JAVA.INTERFACES9;

public class Waveform {
private static long counter;
Private final Long id = counter++;
Public String toString () {
Return "waveform" +ID;
}
}

public class filter{
Public String name () {
Return This.getclass (). Getsimplename ();
}
}

public class Lowerpass extends Filter {
Double cutoff;
Public Lowerpass (double cutoff) {
This.cutoff = cutoff;
}
Public waveform process (waveform input) {
return input;
}
}

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

public class Bandpass extends Filter {
Double Lowcutoff,hightcutoff;
Public bandpass (double lowcutoff,double hightcutoff) {
This.lowcutoff = Lowcutoff;
This.hightcutoff = Hightcutoff;
}
Public waveform process (waveform input) {
return input;
}
}

Filter has the same interface element as processor, but because it is not inherited from processor--because the creator of the filter class is not sure that you want to use it as a processor--so you cannot use the filter for apply.process () method. This is mainly because the Apply.process method and the processor before the coupling is too tight, has exceeded the required program, which makes use of apply.process () code, the use is forbidden. In addition, their inputs and outputs are waveform.

However, if processor is an interface, these restrictions become loose, allowing you to reuse the apply.process () of the interface. Here are the modified versions of processor and apply:

Package COM.EBAO.JAVA.INTERFACES9;

Public interface Processor02 {
String name ();
Object process (object input);
}

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

Public abstract class Stringprocessor implements Processor02 {

Public String name () {
Return This.getclass (). Getsimplename ();
}

Public abstract String Process (Object input);
public static String s = "Disagreement with beliefs are by definition incorrect";
public static void Main (string[] args) {
Apply02.process (New Upcase02 (), s);
Apply02.process (New Downcase02 (), s);
Apply02.process (New Splitter02 (), s);
}
}

public class Upcase02 extends Stringprocessor {

Public String process (Object input) {
return (String) input. toUpperCase ();
}
}

public class Downcase02 extends Stringprocessor {

Public String process (Object input) {
return (String) input. toLowerCase ();
}
}

public class Splitter02 extends Stringprocessor {

Public String process (Object input) {
Return arrays.tostring ((String) input). Split (""));
}
}

An abstract class can inherit an interface without inheriting an interface method.

However, you will often encounter situations where you cannot modify the classes you want to use. For example, in an example of an electronic filter, a class library is found rather than constructed. In these cases, you can use adapter design mode. The code in the adapter will accept the interface you have and produce the interface you need, just like this:

Package COM.EBAO.JAVA.INTERFACES9;

public class Filteradapter implements PROCESSOR02 {
Filter filter;
Public Filteradapter (filter filter) {
This.filter = filter;
}

Public String name () {
return Filter.name ();
}

Public waveform process (Object input) {
Return Filter.process ((waveform) input);
}
}

public class Filterprocessor {
public static void Main (string[] args) {
Waveform w = new waveform ();
Apply02.process (New Filteradapter (New Lowerpass (1.0)), W);
Apply02.process (New Filteradapter (New Highpass (2.0)), W);
Apply02.process (New Filteradapter (New Bandpass (3.0,4.0)), W);
}
}

In the way this adapter is used, the Filteradapter constructor accepts the interface filter you have, and then generates the processor interface object that you want. You may also notice that the agent is used in the Filteradapter class. Decoupling the interface from a specific implementation makes the interface suitable for many different implementations, so the code is more available.

For example, to write another example class, there is a way to adapt it

Package COM.EBAO.JAVA.INTERFACES9;

public class Example {
Public String Exchange (string s) {
Return "Example:" +s;
}
}

public class Exampleadapter implements PROCESSOR02 {
Private Example EP;
Public Exampleadapter (Example EP) {
THIS.EP = EP;
}

Public String name () {
Return Ep.getclass (). Getsimplename ();
}

Public String process (Object input) {
Return Ep.exchange (String) input;
}
}

public class Exampleprocessor {
public static void Main (string[] args) {
Apply02.process (New Exampleadapter (New Example ()), "fengying");
}
}

Operation Result:

Using Processor Example
Example:fengying

Java fully decoupled [9.4 of programming ideas]

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.