Why should the template pattern in the Java design pattern use abstract classes instead of interfaces?
Why should the template pattern in the Java design pattern use abstract classes instead of interfaces?
because the hooks exposed by the template pattern are part of the life cycle (or processing flow).
obviously the interface doesn't let you do some things first, and then some of them (judge? Persistence? The data structure required to transform from an external data structure to a template? And so on) leave room for others to do .
Theinterface just tells others what I can do, but I don't know how to do it , which is defined by the interface's implementation class.
Abstract class is I've done most of it, so you can ok me with some details about what to do .
is very simple, the template contains some common business design, which contains a lot of common methods. The interface cannot contain methods (Java 8's interface supports default and static methods, blurring the boundaries of abstract classes, but it is still impossible to add various public/protected methods as freely as abstract classes.)
interface can not define variables, can only define constants. The
interface method is not specifically implemented. Abstract classes can have concrete implementations.
and. You can still think so. If you want to use a template, use a template. Instead of using multiple templates together. Interfaces can be implemented more often. Abstract classes can only inherit
because the parent class needs to do something
, while the interface parent class is that does nothing
Previous: DrawerNavigator in React-navigation cannot navigate through the onPress pull-out drawer?
Next: The problem of using JNA to call .so file under centos7