Definition: An abstract class exposes the way/template to execute it. Its subclass can be implemented as needed, but the call will be done in the way defined in the abstract class
Features:
1. Encapsulate the unchanged part and expand the variable part.
2. Extract public code for easy maintenance.
3. The behavior is controlled by the parent class and implemented by the child class.
Applications in enterprise-level development and common frameworks: Implementation of hibernate in spring (open transactions, open sessions, close sessions)
Example: Let’s take the example of playing games. There are two operations when playing all games: opening and closing the game, but the way of playing in the middle is different. Let’s see the actual example below.
public class Demo { public static void main(String[] args) { Game g1 = new LOL(); Game g2 = new CF(); g1.playGame(); g2.playGame(); }}abstract class Game{ protected void init(){ System.out.println("Initialize the game and log in to the game... "); } public abstract void play(); protected void end(){ System.out.println("End the game and log out... "); } public void playGame(){ init(); play(); end(); }}class LOL extends Game{ public void play() { System.out.println("play League of Legends Game"); }}class CF extends Game{ public void play() { System.out.println("play Chuang Crossfire Game"); }}The template pattern is actually an abstract application. This pattern is relatively simple and is used more frequently in actual development.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.