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