The most commonly used place is to implement the template method (Template Method)? For example, you want to write a batch of local proxies for remote services. For this batch of agents, the steps of obtaining the request address, sending the request, and obtaining the corresponding content are all common. Only the two steps of defining the character encoding type and defining the response callback are different.
At this time, you can define an abstract class and complete the request process. The differences are left to the actual subclasses to define. It's not that the designer [doesn't allow] the abstract class to be instantiated, but that some attributes cannot be determined before knowing the characteristics of the remote service, making instantiation impossible.
The example in JDK is java.io.InputStream. Its read(byte[], int, int) method calls the java.io.InputStream.read() method. Read() is an abstract method and needs to be defined in a subclass. You can learn from the source code.
public abstract int read() throws IOException;
public int read(byte b[], int off, int len) throws IOException {
if (b == null) {
throw new NullPointerException();
} else if (off < 0 || len < 0 || len > b.length - off) {
throw new IndexOutOfBoundsException();
} else if (len == 0) {
return 0;
}
int c = read();
if (c == -1) {
return -1;
}
b[off] = (byte)c;
int i = 1;
try {
for (; i < len ; i++) {
c = read();
if (c == -1) {
break;
}
b[off + i] = (byte)c;
}
} catch (IOException ee) {
}
return i;
}
It should not be said that abstract classes without abstract methods have special meaning, but the ordinary meaning of abstract classes. Given a default implementation, there may be less code than directly implementing the interface. On the other hand, what is the point of requiring an abstract class to retain one or two abstract methods?
On the contrary, in order for subclasses of this class to create certain methods. Of course, this is also a requirement of the interface, but the interface requires all methods to be abstract methods. But abstract classes can predefine some methods that do not need to be overridden by subclasses.
Abstract classes and interfaces are all for abstraction. I won’t talk about the benefits of abstraction. For example, we want to implement a Equal接口或是类,定义两个方法 equal 和 unequal。如果用接口实现,一个类必须实现这两个方法。可是如果用抽象类实现,可以把unequal在抽象类里实现为!equal,这样在其子类里就需要再实现unequal.
Personally, I prefer abstract classes (but Java不支持多继承)。比如Compareable接口其实还有一个要求,如果a<b && b < c 则 a < c,接口里不能保证这是正确的。当然抽象类也解决不了,但是对于上面的Equal, the implementation of abstract classes is more in line with additional requirements.
Common global properties request response session Common method getLoginUser() Of course there will be others (some common properties and methods). Since there is a controller, service dao can also be used. I usually extract some reset code into the parent class for use.
The most commonly used place is to implement the template method (Template Method)?
For example, you want to write a batch of local proxies for remote services. For this batch of agents, the steps of obtaining the request address, sending the request, and obtaining the corresponding content are all common. Only the two steps of defining the character encoding type and defining the response callback are different.
At this time, you can define an abstract class and complete the request process. The differences are left to the actual subclasses to define. It's not that the designer [doesn't allow] the abstract class to be instantiated, but that some attributes cannot be determined before knowing the characteristics of the remote service, making instantiation impossible.
The example in JDK is java.io.InputStream. Its read(byte[], int, int) method calls the java.io.InputStream.read() method. Read() is an abstract method and needs to be defined in a subclass. You can learn from the source code.
You have to see if it inherits any classes or implements any interfaces
Most likely some shared methods are placed
If not, why not change it to an interface
Some public methods and instance fields may have been extracted.
It should not be said that abstract classes without abstract methods have special meaning, but the ordinary meaning of abstract classes. Given a default implementation, there may be less code than directly implementing the interface. On the other hand, what is the point of requiring an abstract class to retain one or two abstract methods?
On the contrary, in order for subclasses of this class to create certain methods. Of course, this is also a requirement of the interface, but the interface requires all methods to be abstract methods. But abstract classes can predefine some methods that do not need to be overridden by subclasses.
Abstract classes and interfaces are all for abstraction. I won’t talk about the benefits of abstraction. For example, we want to implement a
Equal
接口或是类,定义两个方法equal
和unequal
。如果用接口实现,一个类必须实现这两个方法。可是如果用抽象类实现,可以把unequal
在抽象类里实现为!equal
,这样在其子类里就需要再实现unequal
.Personally, I prefer abstract classes (but
Java
不支持多继承)。比如Compareable
接口其实还有一个要求,如果a<b && b < c
则a < c
,接口里不能保证这是正确的。当然抽象类也解决不了,但是对于上面的Equal
, the implementation of abstract classes is more in line with additional requirements.Example:
Based on: spring mvc
Common global properties request response session
Common method getLoginUser()
Of course there will be others (some common properties and methods).
Since there is a controller, service dao can also be used.
I usually extract some reset code into the parent class for use.