Design Patterns-Template methods

Source: Internet
Author: User

Structure diagram:

Realize:

1 Abstract classAbstractClass2 {3     Abstract voidPrimitiveOperation1 ();//deferred to subclass implementation4     Abstract voidPrimitiveOperation2 ();//deferred to subclass implementation5     6      Public voidTemplatemethod ()7     {8 PrimitiveOperation1 ();9 PrimitiveOperation2 ();Ten          One     } A      - } -  the classConcreteclassaextendsabstractclass{ -  -     voidPrimitiveOperation1 () -     { +System.out.println ("Concrete Class A method one implementation"); -     } +      A     voidPrimitiveOperation2 () at     { -System.out.println ("Concrete Class A method two implementations"); -     } - } -  - classConcreteclassbextendsabstractclass{ in     voidPrimitiveOperation1 () -     { toSystem.out.println ("Concrete class B method one implementation"); +     } -      the     voidPrimitiveOperation2 () *     { $System.out.println ("Concrete class B Method II Implementation");Panax Notoginseng     } - } the  +  Public classTemplatemethod { A      Public Static voidMain (string[] args) the     { + AbstractClass C; -      $c =NewConcreteclassa (); $ C.templatemethod (); -  -c =NewCONCRETECLASSB (); the C.templatemethod (); -     }Wuyi      the      -}

Application:

The template method abstracts the repeated behavior into the parent class, and some specific implementations of the algorithm framework are defined by subclasses, increasing the code reuse rate.

Design Patterns-Template methods

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.