Design Mode-Python version (7)-template method mode

Source: Internet
Author: User

Template Method mode:

Application features: repeat the same logic, but the specific details are different scenarios.

Structure Features: the same logic is extracted to the parent class, and specific details are left on the Child class. Abstract Logic.

UML:

 
#! /Usr/bin/ENV Python # encoding: UTF-8 class template: def _ init _ (Self): Pass def logic (Self): Print 'Do something before .... 'print self. do_something_now () print 'Do something after .... 'def do_something_now (Self): Return none class apply_temp1 (Template): def _ init _ (Self): Pass def do_something_now (Self ): return 'apply 1' class apply_temp2 (Template): def _ init _ (Self): Pass def do_something_now (Self ): return 'apply 2' if '_ main _' = _ name __: obj1 = apply_temp1 () obj2 = apply_temp2 () obj1.logic () obj2.logic () print obj1. _ class _ print obj2. _ class __

Result:

Do something before .... apply 1do something after .... do something before .... apply 2do something after .... _ main __. apply_temp1 _ main __. apply_temp2

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.