Dahua design mode-Template Method Mode _ design mode

Source: Internet
Author: User

In Template method mode (Templatemethod pattern), an abstract class exposes the way/template that defines the method in which it is executed. Its subclasses can override the method implementation as needed, but the invocation will be done in the way defined in the abstract class. This type of design pattern belongs to the behavioral pattern.

The definition of geoscience in the design pattern is: Template method pattern: Defines the skeleton of an algorithm in an operation, and delays some steps into subclasses. Template methods enable subclasses to redefine certain steps of the algorithm without altering the structure of an algorithm.

There is a frequently used example is the support of the Hibernate in Spirng, some of the methods have been set up, such as open the transaction, get session, close session, etc., we do not have to repeat the code that has been standardized, directly give an entity can be saved.

The template method pattern is structured as follows:

The code example is as follows:

The AbstractClass abstract class is actually an abstract template that defines and implements a template method. This template method is generally a concrete method, which gives the skeleton of a top-level logic, while the logical composition step is deferred to the subclass implementation in the corresponding abstract operation, and the top-level logic may invoke some concrete methods.

Abstract class that implements some template methods public
class abstract abstractclass{

    ///Some abstract behavior that is placed in subclasses to implement public
    abstract void PrimitiveOperation1 ();
    public abstract void PrimitiveOperation2 ();

    The template method gives the logical skeleton, and the logic is composed of some corresponding abstract operations, and they all defer to the subclass to implement the public
    final void Templatmethod () {

        initialize ();
        PrimitiveOperation1 ();
        PrimitiveOperation2 ();

    }

The Concreteclass class implements one or more abstract methods defined by the parent class. Each abstractclass can have any number of concreteclass corresponding to it, and each concreteclass can give the different implementations of these abstract methods (that is, the different implementations of the top-level logical constituent steps), so that the implementation of the top-level logic varies.

Note the order of methods in the Concreteclass class and the order of the methods in the template class, and the specific logical implementation order is related to the logical order defined in the template.

An implementation class of an abstract class A public
class Concreteclassa extends abstractclass{

    @override public
    Void PrimitiveOperation2 () {

        System.out.println ("Class A concrete implementation Method 2");

    }

     @Override public
    Void Initialize () {
         System.out.println ("Cocreteclassa initialized");
    }

    @override public
    void PrimitiveOperation1 () {

        System.out.println ("Class A concrete implementation Method 1");

    }

An implementation Class B public
class CONCRETECLASSB extends abstractclass{of an abstract class

    @override public
    Void PrimitiveOperation2 () {

        System.out.println ("Class B Concrete Implementation Method 2");

    }

     @Override public
    Void Initialize () {
         System.out.println ("COCRETECLASSB initialized");
    }

    @override public
    void PrimitiveOperation1 () {

        System.out.println ("Class B Concrete Implementation Method 1");

    }


Test method public
static void Main (string[] args) {

    abstractclass ac;

    AC = new Concreteclassa ();
    Ac. Templatmethod ();

    AC = new CONCRETECLASSB ();
    Ac. Templatmethod ();

}

Run Result:

Cocreteclassa initialized
Class A concrete Realization Method 1
Class A concrete realization Method 2

COCRETECLASSB initialized
Class B Concrete Realization Method 1
Class B Concrete Implementation Method 2

Features of template method patterns:

1, template method mode is by moving the invariant behavior to the superclass, the removal of duplicate code in the subclass to reflect its advantages;

2, template method mode is to provide a good code reuse platform; sometimes we run into a sequence of steps that we need to execute, and the process is the same at a high level, but some of the steps can be implemented differently.

3. When invariant and variable behaviors are mixed together in the method subclass implementation, the invariant behavior is repeated in subclasses, and we move these behaviors to a single place through the template method pattern, which helps the subclass to get rid of the entanglement of repetitive invariant behavior.

Advantages:

1, the package invariant part, expands the variable part.

2, the extraction of public code, easy to maintain.

3, the behavior is controlled by the parent class, the subclass realizes.

Disadvantage: Each different implementation requires a subclass to achieve, resulting in the number of classes increased, making the system even larger.

Usage scenarios:

1, there are many subclasses common methods, and the same logic.

2, important, complex methods, you can consider as a template method.

Note: To prevent malicious action, the general template method plus final keyword.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.