Behavioral patterns - Template Mode

Template mode

Pattern mode (Template Pattern), one abstract class defines its disclosed method / template. Its subclass can override methods as needed to achieve, but the invocation will be defined in the abstract class.

Introduction

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.

achieve

We will create a custom action in  Game  abstract class, in which the template method is set to final, so it will not be overwritten. Cricket  and  Football  is an extension of the  Game  entity class which overrides the abstract class method.

TemplatePatternDemo , our demonstration classes using  Game  to demonstrate the use of a template pattern.

 

 1, to create an abstract class that the template method is set to final

public abstract class Game {

    abstract void initialize();
    abstract void startPlay();
    abstract void endPlay();

    public final void play(){
        initialize();

        startPlay();

        endPlay();
    }
}

2, to create an extended entity classes above class

public class Cricket extends Game {

    @Override
    void initialize() {
        System.out.println("Cricket Game Initialized! Start Playing.");
    }

    @Override
    void startPlay() {
        System.out.println("Cricket Game Started! Enjoy the game.");
    }

    @Override
    void endPlay() {
        System.out.println("Cricket Game Finished!");
    }
}
public class Football extends Game {

    @Override
    void initialize() {
        System.out.println("Football Game Initialized! Start Playing.");
    }

    @Override
    void startPlay() {
        System.out.println("Football Game Started! Enjoy the game.");
    }

    @Override
    void endPlay() {
        System.out.println("Football Game Finished!");
    }
}

3, using the template method of play Game () is defined to demonstrate the way the game

public class TemplatePatternDemo {

    public static void main(String[] args) {
        Game game = new Cricket();
        game.play();

        System.out.println();

        game = new Football();
        game.play();
    }
}

Export

 

 Reference: https://www.runoob.com/design-pattern/template-pattern.html

Guess you like

Origin www.cnblogs.com/hoo334/p/12529506.html