Interface embodies a kind of design philosophy of specification and realization separation, which can greatly reduce the coupling between modules of program, and improve the scalability and maintainability of the system.
Here we introduce Java-oriented interface programming simple factory model
There is a scenario: Suppose a computer class in the program needs to assemble an output device.
Computer class: You need to initialize an output device, pass something that needs to be printed to an output device, and output what you want to print.
Package Fang;
public class Computer {
private Output out;
Public Computer [Output out] {
this.out= out;
}
public void Keyin (String msg) {
out.getdata (msg);
}
public void print () {
out.out ();
}
}
Output interface: an interface for connecting computer classes and output devices (such as printers, and so on). Computer pass the output interface to the printed object (GetData ()) and output The thing to be printed (out ()).
Package Fang;
Public interface Output {
int max_cache_line = m;
void out ();
void GetData (String msg); default void Print (String ... msgs) {
//For (string msg:msgs) {
// System.out.println (msg); }
//// default void Test () {
// System.out.println ("The Default Test"); }
// //this static method of interface Output can is accessed as output.statictest
// St atic String statictest () {
// System.out.println ("Static statictest in InterFace");
return "Static statictest in InterFace"; }
// private void foo () {
// System.out.println ("Private foo"); }
// private static void Bar () {
// System.out.println ("Bar private Static");
// }
}
Outputfactory class: (First look at the code, we re-analysis)
Package Fang;
public class Outputfactory {public
Output getoutput () {return
new Printer ();
}
public static void Main (string[] args) {
outputfactory of = new Outputfactory ();
Computer C = new Computer (Of.getoutput ());
C.keyin ("Advanced mathematics");
C.keyin ("Probability Theory and Mathematical Statistics");
C.print ();
}
The important code is the GetOutput () function, because it returns an instance of a printing device.
If we want to add a printer () to the Computer class, then GetOutput () returns the printer;
If we want to add betterprinter () to the Computer class, then GetOutput () returns Betterprinter.
Printer class:
package Fang; interface product{int getproducttime ();} public class Printer implements output,product{private string[] Printdata
= new String [Max_cache_line];
private int datanum=0;
public void out () {while (datanum>0) {System.out.println ("Printer print job:" +printdata[0]);
System.arraycopy (Printdata, 1, printdata, 0,--datanum);
} public void GetData (String msg) {if (datanum>= max_cache_line) {System.out.println ("The output queue is full, add failed");
else {printdata[datanum++]= msg;
System.out.println (Datanum);
} public int Getproducttime () {return 45; }//public static void Main (String []args)//{//Output o = new Printer ();//O.getdata ("University mathematics");//O.getdata ("University Physics")
);
O.out ();
Output.statictest ();
O.getdata ("Probability Theory and Mathematical Statistics");
O.getdata ("Complex variable function");
O.out ();
O.print ("Monkey King", "Pig", "Tang's Monk");
O.test ();
Product p= new Printer ();
System.out.println (P.getproducttime ());
Object obj = p; // }
}
Betterprinter class:
Package Fang;
public class Betterprinter implements Output {
private string[] Printdata = new String [max_cache_line*2];
private int datanum=0;
public void out () {while
(datanum>0) {
System.out.println ("The high-speed printer is printing:" +printdata[0]);
System.arraycopy (Printdata, 1, printdata, 0,--datanum);
}
public void GetData (String msg) {
if (datanum>= max_cache_line*2) {
System.out.println ("The output queue is full, add failed");
}
else {
printdata[datanum++]= msg;
System.out.println (Datanum);}}
Run Result:
Situation one: Outputfactory output is printer equipment, print results see below:
Printer print jobs: Higher math
printer print jobs: probability theory and mathematical statistics
Situation Two: Outputfactory output is betterprinter equipment, print results see below:
High-speed printer printing: High math
High speed printer is printing: probability theory and mathematical statistics
This way, you can centralize the logic of all generated output objects in the Outputfactory factory class, and all classes that require an output object need to be coupled to the output interface rather than to its specific implementation class. Even if there are many classes in the system that use the printer object, as long as the output objects generated by the GetOutput () method of the Outputfactory class are Betterprinter objects, they will all use the Betterprinter object instead, All programs need not be modified, just modify the Outputfactory factory class GetOutput () method implementation.