Template mode, as the name implies, is through the way of template rubbing.
Defining templates is defining frameworks, structures, and prototypes. Define an agreement that we always abide by.
Defining the template, our remaining work is to enrich it, enrich it, and improve its shortcomings.
Definition templates are defined by abstract classes. Common structured logic needs to be completed in abstract classes. Only non-public partial logic is abstracted into abstract methods, leaving the subclasses to enrich the implementation.
So the shortcomings mentioned above are these abstract methods.
In general, a template pattern defines a logical template through an abstract class, a logical framework, a logical prototype, and then abstracts the indeterminate parts into abstract classes for subclasses to implement. Generally, the invocation logic of these abstract classes is still done in abstract classes. So the template is to define a frame ratio.For example, when building a house, we define a template: the house should be closed, there are doors, windows and so on, but what kind of doors, what kind of windows, these are not described in the template, this sub-category to perfect, such as doors using anti-theft doors, windows using northward windows and so on.
Let’s see how template mode can be seen in building housing as an example.
Template Abstract Class: HouseTemplate
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 //Public logic18 public final void buildHouse () {Nineteen20 buildBase ();21 buildWall ();Twenty-twoBuildDoor ();23 buildWindow ();Twenty-four25}Twenty-six27}
Subclass 1:HouseOne
2 3 HouseOne(String name){ 4 super(name); 5 } 6 7 @Override 8 protected void buildDoor() { 9 System.out.println(name +"The door must be used for security doors.10}Eleven12 @Override13 protected void buildWindow () {14 System.ouT.println (name + "windows face north");15}Sixteen17 @Override18 protected void buildWall () {NineteenSystem.out.println (name + wall is built of marble).20}Twenty-one22 @Override23 protected void BUildBase () {24 System.out.println (name + "foundation for the use of iron and steel foundation");25}Twenty-six27}
Subclass 2:HouseTwo
1 public class HouseTwo extends HouseTemplate { 2 3 HouseTwo(String name){ 4 super(name); 5 } 6 7 @Override 8 protected void buildDoor() { 9 System.out.println(name + "The door adopts wooden door ".10}Eleven12 @Override13 protected void buildWindow () {14 System.out.Println (name + "windows to the South");15}Sixteen17 @Override18 protected void buildWall () {NineteenSystem.out.println (name + wall is made of glass).20}Twenty-one22 @Override23 protected void buildBASE () {24 System.out.println (name + "foundation used granite");25}Twenty-six27}
Test class: Clienter
1 public class Clienter { 2 3 public static void main(String[] args){ 4 HouseTemplate houseOne = new HouseOne("House 1 ");5 HouseTemplate houseTwo = new HouseTwo ("house 2");6 houseOne.buildHouse ();SevenHouseTwo.buildHouse ();8}Nine10}
Test results:
The foundation of house 1 uses steel foundation.The wall of house 1 is built of marble.The door of house 1 is to use anti-theft door.1 of the windows of the house face north.The foundation of the house 2 uses granite.The walls of house 2 are made of glass.The door of house 2 is wooden door.The windows of house 2 will face south.
Subclass 1:HouseOne
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 +"The door must be used for security doors.17}Eighteen19 @Override20 protected void buildWindow () {21 System.ouT.println (name + "windows face north");22}Twenty-three24 @Override25 protected void buildWall () {Twenty-sixSystem.out.println (name + wall is built of marble).27}Twenty-eight29 @Override30 protected void BUildBase () {31 System.out.println (name + "foundation for the use of iron and steel foundation");32}Thirty-three34 @OverrideThirty-fiveProtected void buildToilet () {36 System.out.println (name + "toilets built in southeast corner");37}Thirty-eightThirty-nine@Override40 protected Boolean isBuildToilet () {41 return isBuildToilet;42}Forty-three44}
Subclass 2:HouseTwo
public class HouseTwo extends HouseTemplate { HouseTwo(String name){ super(name); } @Override protected void buildDoor() { System.out.println(name + "The door adopts wooden door ".}@OverrideProtected void buildWindow () {System.out.println (name +)"The windows need to go south".}@OverrideProtected void buildWall () {System.out.println (name +)The walls are made of glass "";}@OverrideProtected void buildBase () {System.out.println (name +)"The foundation uses granite".}@OverrideProtected void buildToilet () {System.out.println (name)+ "toilets built in northwest corner");}}
Test class: Clienter
1 public class Clienter { 2 3 public static void main(String[] args){ 4 HouseTemplate houseOne = new HouseOne("House 1, "false";5 HouseTemplate houseTwo = new HouseTwo ("house 2");6 houseOne.buildHousE ();7 houseTwo.buildHouse ();8}Nine10}
Test results:
The foundation of house 1 uses steel foundation.
The wall of house 1 is built of marble.
The door of house 1 is to use anti-theft door.
1 of the windows of the house face north.
The foundation of the house 2 uses granite.
The walls of house 2 are made of glass.
The door of house 2 is wooden door.
The windows of house 2 will face south.
2 of the house’s toilets are built in the northwest corner.