Java design mode Template method mode

Source: Internet
Author: User

The overview template method pattern is the behavior pattern of the class. Prepare an abstract class, implement some of the logic in the form of concrete methods and concrete constructors, and then declare some abstract methods to force subclasses to implement the remaining logic. Different subclasses can implement these abstract methods in different ways, thus having different implementations of the remaining logic. This is the intent of the template method pattern. For example, the skeleton of an algorithm in an operation is defined, and the steps are deferred to the subclass. The template method allows subclasses to redefine some specific steps of an algorithm without altering the structure of an algorithm. The Role abstract class (AbstractClass) in the pattern: the template method is implemented, and the skeleton of the algorithm is defined. Concrete Class (Concreteclass): Implements the abstract method in the abstract class, has completed the complete algorithm. Specific application: Some column operation before the simulation person goes to work
 PackageCom.blankjor.templatemethod;/*** @desc Abstract Human method *@authorblankjor * @date June 11, 2017 pm 10:09:18*/ Public Abstract classAbstractperson {/*** Define a way to work a series of operations before going to work the final method does not allow changes to go to work*/    Final voidWork () {//Get upRise (); //Wear clothesdress (); //have breakfastbreakfast (); //If you're a girl, make up.        if(Iswoman ()) {makeUp (); }    }    /*** Things to do in the public*/    voidrise () {System.out.println (Get up); }    voiddress () {System.out.println ("Get dressed."); }    voidBreakfast () {System.out.println ("Breakfast"); }    //waiting for the sub-class to realize    Abstract voidmakeUp (); //hook functionBoolean Iswoman () {return true; }} PackageCom.blankjor.templatemethod;/*** @desc *@authorblankjor * @date June 11, 2017 pm 10:17:44*/ Public classMansextendsAbstractperson {@OverridevoidmakeUp () {//No makeup .} @Override Boolean Iswoman () {return false; }} PackageCom.blankjor.templatemethod;/*** @desc *@authorblankjor * @date June 11, 2017 pm 10:17:44*/ Public classWomanextendsAbstractperson {@OverridevoidmakeUp () {System.out.println (Makeup); } @Override Boolean Iswoman () {return true; }} PackageCom.blankjor.templatemethod;/*** Test method for @desc Template method mode *@authorblankjor * @date June 11, 2017 pm 10:19:18*/ Public classMaintest { Public Static voidMain (string[] args) {Mans man=NewMan (); Woman Woman=NewWoman (); System.out.println ("Men go to Work");        Man.work (); System.out.println ("Women go to work");    Woman.work (); }}

Operation Result:

The advantage Template method pattern removes duplicate code from subclasses by moving the invariant behavior to the superclass. Subclasses implement some of the details of the algorithm, which facilitates the expansion of the algorithm. By invoking the subclass implementation of a parent class, the new behavior is added through the subclass extension, which conforms to the open-close principle. Disadvantages each of the different implementations needs to define a subclass, which causes the number of classes to increase and the design to be more abstract. Applicable scenarios in some classes of algorithms, using the same method, resulting in code duplication. Control subclass extensions, subclasses must adhere to algorithmic rules. Reference: http://blog.csdn.net/jason0539/article/details/45037535

Java design mode Template method mode

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.