This article reprinted address: http://www.cnblogs.com/iyangyuan/archive/2013/03/11/2954808.html
In the process of programming, the reader is likely to encounter such a dilemma: the design of an interface, but the implementation of the sub-class interface does not need to implement all the methods in the interface, that is, there are too many methods in the interface, for some subclasses is redundant, we have to waste to write an empty implementation.
The "abstract interface" mentioned today is used to solve this problem.
To not mislead the reader, first explain what is an "abstract interface."
The so-called "abstract interface", that is, provides an interface while providing an abstract class, with an abstract class implementation of the interface (in fact, this is the default adaptation mode).
Here are some examples of the benefits that the reader has to make.
The code does not write, in order to prevent readers do not understand, first on a class diagram:
Specific code:
Itestinterface.java
1/*2 assumes that there is a top-level Interface 3 */4 public interface itestinterface{5 void Method1 (); 6 int method2 (); 7 Boolean Method3 (); 8}
Testabstract.java
1/* 2 Abstract class abstraction implements Itestinterface top-level Interface 3 */4 5 Public abstract class Testabstract implements itestinterface{6
//find the necessary method in the interface, that is, the subclass must implement the method, defined as an abstract method, by the subclass implementation 7 public abstract void method1 (); 8 public Abstract int METHOD2 (); 9 ///Some unique methods can be implemented by default in abstract class One public boolean method3 () { return true;13 }14}
Testclass1.java
1/* 2 Ordinary class TestClass1 inherits Testabstract abstract Class 3 */4 5 public class TestClass1 extends testabstract{6 7 //TESTC Lass1 must implement an abstract Method1 method, which is initially defined in the interface 8 public void Method1 () {9 }11 //testclass1 must implement an abstract Method2 method, The method is first defined in the interface in the public int method2 () { 1;14 }15 //Interface Method3 method is irrelevant for TESTCLASS1, So do not rewrite. 17}
Testclass2.java
1/* 2 Ordinary class TestClass2 inherits Testabstract abstract Class 3 */4 5 public class TestClass2 extends testabstract{6 7 //TESTC Lass2 must implement an abstract Method1 method, which is initially defined in the interface 8 public void Method1 () {9 }11 //testclass2 must implement an abstract Method2 method, This method is first defined in the interface, the public int method2 () { 2;14 }15 //method3 method is essential for TESTCLASS2, It must therefore be rewritten. + Public Boolean method3 () { return false;19 }20 21}
Code explaining:
As can be seen from the above example, the interface of the highest layer is implemented by an abstract class, in which we define the key method1 and Method2 methods as abstract methods, forcing subclasses to implement, and the "unique" method3 method to do a default implementation in the abstract class.
Wait until TestClass1, TestClass2 inherit Testabstract abstract class, the advantage is manifested, TESTCLASS1, TestClass2 must implement METHOD1, METHOD2, but if not used method3, can be ignored directly.
Through the combination of interfaces and abstract classes, avoids a large number of "meaningless" implementations in the subclasses that implement the interface, this "meaningless" implementation is buffered into an abstract class, which perfectly demonstrates the code reuse (which can be understood as the buffer between the interface and the implementation class).
It should be pointed out that we can either inherit the abstract class or choose to implement the interface, not necessarily to inherit the abstract class, see the situation, here are two options, two opportunities.
Write to this, perhaps readers think the article is over, in fact, there is no ...
The benefit of doing this is not only this, but savor it if we add a method to the interface ...
Specific code:
warm tip: do not be frightened by the code, in fact, the code and the top of the same, but added a method just.
Itestinterface.java
1/* 2 assume there is a top-level interface 3 */4 public interface itestinterface{5 void Method1 (); 6 int method2 (); 7 Boolean meth Od3 (); 8 //New method added in interface 9 String method4 (); 10}
Testabstract.java
1/* 2 Abstract class abstraction implements Itestinterface top-level Interface 3 */4 5 Public abstract class Testabstract implements itestinterface{6
//find the necessary method in the interface, that is, the subclass must implement the method, defined as an abstract method, by the subclass implementation 7 public abstract void method1 (); 8 public abstract int method2 (); 9 //Some unique methods can be implemented by default in the abstract class one by one public Boolean method3 () { return true;13 }14 // A default implementation is provided in the abstract class to avoid "alerting" all subclasses to the public String method4 () {" " "; }19}
Testclass1.java
1/* 2 Ordinary class TestClass1 inherits Testabstract abstract Class 3 */4 5 public class TestClass1 extends testabstract{6 7 //TESTC Lass1 must implement an abstract Method1 method, which is initially defined in the interface 8 public void Method1 () {9 }11 //testclass1 must implement an abstract Method2 method, The method is first defined in the interface in the public int method2 () { 1;14 }15 //Interface Method3 method is irrelevant for TESTCLASS1, So do not rewrite. The new method is important for TESTCLASS1, so you must rewrite the public String method4 () { return "Class1"; 22 23}
Testclass2.java
1/* 2 Ordinary class TestClass2 inherits Testabstract abstract Class 3 */4 5 public class TestClass2 extends testabstract{6 7 //TESTC Lass2 must implement an abstract Method1 method, which is initially defined in the interface 8 public void Method1 () {9 }11 //testclass2 must implement an abstract Method2 method, This method is first defined in the interface, the public int method2 () { 2;14 }15 //method3 method is essential for TESTCLASS2, It must therefore be rewritten. $ Public Boolean method3 () { false;19 }20 //New method is irrelevant for TestClass2, No need to know the presence of new METHOD4 22}
Code explaining:
This code shows that if the project has been formed, but the requirements have changed, we have to add a new method to the interface, if the sub-class directly implement the interface, then these subclasses are modified to implement the interface new method.
But in this case, the TESTCLASS1, TestClass2 subclasses do not directly implement the interface, but by inheriting the abstract class to implement the interface indirectly, so that the benefits are reflected!
The new method to the interface can be buffered in the abstract class that implements the interface, providing a default implementation, so that you do not have to enforce all subclasses ( classes that indirectly implement an interface by inheriting an abstract class) are modified so that the image can be interpreted as "without disturbing the child class". Instead, you need to use the subclass of this method to rewrite it directly.
Side-dishes feeling:
Man's wisdom is great! Arrays and linked lists combine to produce an efficient hash table, and interfaces and abstract classes combine to produce an elegant default adaptation pattern. Everybody work hard!!!
Written in the following words:
There is no perfect thing in the world, the design pattern is so, too much discussion pros and cons no meaning, the right is the best, what is appropriate? This is where wisdom is embodied.
Java design mode-Default adapter mode