Template mode and usage scenario of Java design pattern

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

   1 public class HouseOne extends HouseTemplate {
 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.
Copy code ></span></div>
<pre>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.</pre>
<div class=Copy code ></span></div>
</div>
</div>
<p>  Through the above examples, we have learned the basic method and template method in template pattern, in which the buildHouse method in HouseTemplate is the basic method, and the other four are template methods. The basic method is usually modified with final to ensure that it will not be quilt.Class modification, while the template method is modified with protected, indicating that it needs to be implemented in a subclass.</p>
<p>  In fact, there is also a concept of the hook method in the template pattern. Some people say that the template pattern with the hook method is complete, maybe.</p>
<p>  What does hook method do? A hook is a privilege given to a subclass to override the execution of basic logic by rewriting the hook, which is sometimes very useful. For example, when building a house, there is a need for a subclass to decide whether to build a toilet.</p>
<p>Template Abstract Class: HouseTemplate</p>
<div 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 
19     //Hook Method20 protected Boolean isBuildToilet () {21 return true;22}Twenty-three24 / / public logicTwenty-fivePublic final void buildHouse () {Twenty-six27 buildBase ();28 buildWall ();Twenty-nineBuildDoor ();30 buildWindow ();31 if (isBuildToilet ()) {32 buildToilet ();33}34}Thirty-five36}

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.

Copy code ></span></div>
<pre>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.House 2 toiletBuilt in the northwest corner</pre>
<div class=Copy code ></span></div>
</div>
</div>
<p>  As we can clearly see from the direct results, we customized house 1 by rewriting the hook without building a fasle.</p>
<p>  The role of hook method is clear at the same time.</p>
<p>  The key points of template mode are:</p>
<p>    1、Using abstract classes to define template classes, and in which all the basic methods, template methods, hook methods, unlimited number, to achieve functional logic based. The basic method uses final modifier, where the basic method and hook method are called, and the basic method and hook method can use protecTed modification indicates that it can be modified by subclasses.</p>
<p>    2、Define subclasses that implement Abstract classes, rewrite template methods, or even hook methods, and refine the logic.</p>
<p>  Usage scenario:</p>
<p>    1、If you have the same methods in multiple subclasses and the logic is the same, you can extract them and put them in a template Abstract class.</p>
<p>    2、The template can be used when the main frame of the program is the same and the details are different.</p>
<p>Original link: https://www.cnblogs.com/V1haoge/p/9558825.html</p>
</div>
	</div><!-- .entry-content -->

	<footer class= Posted on Categories Java

Leave a Reply

Your email address will not be published. Required fields are marked *