Java template pattern

Pattern mode (Template Pattern), the definition of an abstract class is disclosed a method of performing its info / template. Its subclass can override methods as needed to achieve, but the invocation will be defined in the abstract class. This type of design pattern belongs behavioral patterns.

Intent: skeleton algorithm defined in one operation, while deferring some steps to subclasses. Template Method lets subclasses may not change the structure of certain steps of an algorithm of the algorithm to redefine.

The main solution: some of the ways GM, but in each sub-class re-write this approach.

When to use: There are some common methods.

How to solve: abstracting these generic algorithms.

The key code: the abstract class implements, other steps implemented in a subclass.

Application examples: 1, when in a house built, foundations, wiring, water pipes are the same, only in the latter part of the building only difference closet plus plus fences. 2, there Journey Buddha given 81 difficult good, which is a top-level logical skeleton. 3, spring in support for Hibernate, and some have been given good way to encapsulate such as opening a transaction to acquire Session, Session is closed and so on, the programmer does not repeat those already write good code norms, direct throw an entity can be saved.

Advantages: 1, the same part of the package, a variable extension portion. 2, extracts the common code, easy to maintain. 3, the behavior control by the parent class, subclass achieved.

Disadvantages: each requires a different implementation to implement a subclass, resulting in an increase of the number of classes, making the system more bulky.

Be used: 1, a number of methods common subclass, and the same logic. 2, important, complex methods can be considered as a template method.

Note: To prevent malicious operations, general template methods plus final keyword.

Game.java

public abstract class Game {// create an abstract class, his template method is set to Final
    abstract void the initialize ();
    abstract void startPlay ();
    abstract void endPlay ();
    
    // template
    public Final void Play () {
        // initialization game
        the initialize ();
        // start the game
        startPlay ();
        // end of the game
        endPlay ();
    }
}

Cricket.java

public class Cricket extends Game { //创建扩展Game的实体类
    @Override
    void endPlay() {
        System.out.println("Cricket Game Finished");
    }
    
    @Override
    void initialize() {
        System.out.println("Cricket Game Intitialized! Start playing");
    }
    
    @Override
    void startPlay() {
        System.out.println("Cricket Game Started. Enjoy the game");
    }
    
}

Football.java

public class Football extends Game {
    
    @Override
    void endPlay() {
        System.out.println("Football Game Finished!");
    }
    
    @Override
    void initialize() {
        System.out.println("Football Gmae Initialize! Start playing.");
    }
    
    @Override
    void startPlay() {
        System.out.println("Football Game Started. Enjoy the game!");
    }
    
}

TemplatePatternDemo.java

public class TemplatePatternDemo {// use the template method of play Game () to demonstrate the way the game is defined
    
    public static void main (String [] args) {
        
        Game Game = new new Cricket ();
        game.play ();
        System.out.println ();
        Game Football new new = ();
        game.play ();
        
    }
    
}

 

Guess you like

Origin blog.csdn.net/walkerwqp/article/details/91849150
Recommended