------------------------------------------ Behavioral Design Patterns
1. Template mode
Why template model:
A skeleton defined algorithm, and some of the steps will achieve the delay to subclasses. Method template so that subclasses can without changing the configuration of algorithms, embodied redefine certain steps in the algorithm;
advantage:
- Template Method pattern by the same moving behavior of the parent class, subclass to addition to the code repetition;
- Subclass implementation of certain details of the algorithm, the algorithm will help expand;
- Parent calls the operation a subclass to achieve, add a new sub-class behavior by extension, in line with "open - closed principle."
Disadvantages:
-
In template mode, the result subclass affecting the implementation of the results of the parent class, will increase the difficulty of reading the code;
Scenario:
- There are multiple subclasses method, and the same logic;
- Important, complex algorithms, the core algorithm can be designed as a template method
achieve:
Template mode, the default base class method implements the concept is reduced to hook Hooks, they are designed to be overridden in a subclass, if you expect some of the method is not overridden in a subclass, you can get them to Final . E.g,
Template abstract class
1 public abstract class HouseTemplate { 2 3 protected HouseTemplate(String name){ 4 this.name = name; 5 } 6 7 protected String name; 8 9 protected abstract void buildDoor(); 10 11 protected abstract void buildWindow(); 12 13 protected abstract void buildWall(); 14 15 protected abstract void buildBase(); 16 . 17 protected abstract void buildToilet (); 18 is . 19 // hook method 20 is protected Boolean isBuildToilet () { 21 is return to true ; 22 is } 23 is 24 // common logical 25 public Final void buildHouse () { 26 is 27 buildBase (); 28 buildWall (); 29 buildDoor (); 30 buildWindow (); 31 is IF (isBuildToilet ()) { 32 buildToilet (); 33 } 34 } 35 36 }
Subclass 1
1 public class HouseOne extends HouseTemplate { 2 3 HouseOne(String name){ 4 super(name); 5 } 6 7 HouseOne(String name, boolean isBuildToilet){ 8 this(name); 9 this.isBuildToilet = isBuildToilet; 10 } 11 12 public boolean isBuildToilet; 13 14 @Override 15 protected void buildDoor() { 16 System.out.println (name + "anti-theft door to door" ); . 17 } 18 is . 19 @Override 20 is protected void buildWindow () { 21 is System.out.println (name + "for a north facing windows" ); 22 } 23 is 24 @Override 25 protected void buildWall () { 26 is System.out.println (name + "marble wall construction" ); 27 } 28 29 @Override 30 protected void buildBase () { 31 is System.out.println ( name + "foundation using steel foundation."); 32 } 33 is 34 is @Override 35 protected void buildToilet () { 36 System.out.println (name + "toilet built in the southeast corner" ); 37 [ } 38 is 39 @Override 40 protected Boolean isBuildToilet () { 41 is return isBuildToilet ; 42 } 43 is 44 is }
Subclass 2
. 1 public class HouseTwo the extends HouseTemplate { 2 . 3 HouseTwo (String name) { . 4 Super (name); . 5 } . 6 . 7 @Override . 8 protected void buildDoor () { . 9 System.out.println (name + "using wooden door" ) ; 10 } . 11 12 is @Override 13 is protected void buildWindow () { 14 System.out.println (name + "windows to south" ); 15 } 16 . 17 @Override 18 is protected void buildWall () { . 19 System.out.println (name + "manufactured glass wall" ); 20 is } 21 is 22 is @Override 23 is protected void buildBase () { 24 System.out.println (name + " the use of granite foundation " ); 25 } 26 is 27 @Override 28 protected void buildToilet () { 29 System.out.println (name +" in the northwest corner of the toilet building " ); 30 } 31 is 32 }
Client
1 public class Clienter { 2 3 public static void main(String[] args){ 4 HouseTemplate houseOne = new HouseOne("房子1", false); 5 HouseTemplate houseTwo = new HouseTwo("房子2"); 6 houseOne.buildHouse(); 7 houseTwo.buildHouse(); 8 } 9 10 }
It can be seen by overriding the hook method does not require custom built house 1 toilet (fasle), another may refer to this article: Design mode of learning nine notes: template method pattern .