Template Method Mode

Source: Internet
Author: User
Description

Defines the skeleton of an algorithm in an operation, and delays the step to the subclass. The template method allows subclass to redefine certain steps of an algorithm without changing the structure of an algorithm.

Schema Structure

Program example

Note: use the same set of tea and coffee

Code:

class Beverage(object):    """description of class"""    def makeBeverage(self):        self.boilWater()        self.brew()        self.pourInCup()        self.addCondiments()    def boilWater(self):        print ‘boil water‘    def brew(self):        print ‘brew‘    def pourInCup(self):        print ‘pour into cup‘    def addCondiments(self):        print ‘add condiments‘class Coffee(Beverage):    def brew(self):        print ‘coffee‘    def addCondiments(self):        print ‘Adding Sugar and Milk...‘class Tea(Beverage):    def brew(self):        print "tea"    def addCondiments(self):        print ‘Adding Lemon...‘if ‘__main__‘==__name__:    coffee = Coffee()    coffee.makeBeverage()    tea = Tea()    tea.makeBeverage()

Running result:

Reference Source:

Http://www.cnblogs.com/chenssy/p/3679190.html

Http://www.cnblogs.com/wuyuegb2312/archive/2013/04/09/3008320.html

Http://www.cnblogs.com/zhuxiongfeng/archive/2010/04/09/1708615.html

 

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.