Template Pattern Pattern method

Source: Internet
Author: User

The 1> template pattern defines the steps of the algorithm, extending the implementation of these steps to subclasses

2> template mode provides us with a code reuse technique.

3> Template Abstract classes can define specific methods, abstract methods, and Hook methods

4> to prevent subclasses from changing the algorithm in the template, the template method can be declared as final

A 5> hook is a method that does not work in an abstract class, or only does the default, and subclasses can choose to implement it

Template method pattern, which defines the skeleton of an algorithm in one method, and delays some steps into subclasses. The template method allows subclasses to redefine some of the steps in the algorithm without changing the attaining of the algorithm structure.

Examples of our use of brewed coffee and brewing tea

Processing Flow:

Coffee Brewing Method: 1. Boil the water, 2. Brew coffee in boiling, 3. Pour the coffee into the cup, 4. Add sugar and milk

Tea Brewing Method: 1. Boil the water, 2. Brew the tea with boiling, 3. Pour the tea into the cup, 4. Add Honey

Practice steps:

1> Create a template (abstract) class: Beverage (beverage)

Java code
  1. Package com.kaishengit.beverage;
  2. Public abstract class Beverage {
  3. /** 
  4. * Brew coffee or tea ... Process
  5. */
  6. public final void Create () {
  7. Boilwater (); //Boil the water
  8. Brew (); //Brew with boiling water ...
  9. Pourincup (); //Put ... Pour into the cup
  10. Addcoundiments (); //Plus ...
  11. }
  12. public abstract void addcoundiments ();
  13. public abstract Void Brew ();
  14. public void Boilwater () {
  15. System.out.println ("boiled water");
  16. }
  17. public void Pourincup () {
  18. System.out.println ("pour into cup");
  19. }
  20. }

2> Create a coffee class (Coffee) and tea class, all inheriting beverage abstract class

1. Coffee (Coffee)

Java code
  1. Package com.kaishengit.beverage;
  2. Public class Coffee extends beverage{
  3. @Override
  4. public void Addcoundiments () {
  5. System.out.println ("Add sugar and milk"); }  
  6. @Override
  7. public Void Brew () {
  8. System.out.println ("Brew Coffee with water");
  9. }
  10. }

2. Tea (TEAS)

Java code
  1. Package com.kaishengit.beverage;
  2. Public class Tea extends beverage{
  3. @Override
  4. public void Addcoundiments () {
  5. System.out.println ("Add Honey");
  6. }
  7. @Override
  8. public Void Brew () {
  9. System.out.println ("tea with water");
  10. }
  11. }

3. Let's test:

Java code
  1. Package com.kaishengit.beverage;
  2. Public class Test {
  3. public static void Main (string[] args) {
  4. Coffee Coffee = new Coffee ();
  5. Coffee.create (); //Brew coffee
  6. //tea tea = new tea ();//Brewing Tea
  7. //tea.create ();
  8. }
  9. }

Operation Result:

-----------------------------------

Boiled boiled water

Coffee with water

Pour into the cup

Add sugar and milk

-----------------------------------

Using hooks in template mode

There is an empty implementation method, we call this method "hook". Subclasses can decide whether to overwrite it, depending on the situation.

1> we modify the template class (beverage)

Java code
  1. Package com.kaishengit.beverage;
  2. Public abstract class Beverage {
  3. /** 
  4. * Brew coffee or tea ... Process
  5. */
  6. public final void Create () {
  7. Boilwater (); //Boil the water
  8. Brew (); //Brew with boiling water ...
  9. Pourincup (); //Put ... Pour into the cup
  10. Addcoundiments (); //Plus ...
  11. Hook (); //Hooks
  12. }
  13. //Null implementation method
  14. public void Hook () {}
  15. public abstract void addcoundiments ();
  16. public abstract Void Brew ();
  17. public void Boilwater () {
  18. System.out.println ("boiled water");
  19. }
  20. public void Pourincup () {
  21. System.out.println ("pour into cup");
  22. }
  23. }

2> if we engage in activities, drink a cup of coffee, modify the coffee (Coffee) class

Java code
  1. Package com.kaishengit.beverage;
  2. Public class Coffee extends beverage{
  3. @Override
  4. public void Addcoundiments () {
  5. System.out.println ("Add sugar and milk"); }  
  6. @Override
  7. public Void Brew () {
  8. System.out.println ("Brew Coffee with water");
  9. }
  10. /** 
  11. * Hooks
  12. */
  13. @Override
  14. public void Hook () {
  15. System.out.println ("one more Cup");
  16. }
  17. }

3> using the test class above

Operation Result:

--------------------------------

Boiled boiled water

Coffee with water

Pour into the cup

Add sugar and milk

Another drink.

--------------------------------

The result is "one more drink" ...

We can also use hooks like this to determine if the code inside is executing

1> we modify the template class (beverage)

Java code
  1. Package com.kaishengit.beverage;
  2. Public abstract class Beverage {
  3. /** 
  4. * Brew coffee or tea ... Process
  5. */
  6. public final void Create () {
  7. Boilwater (); //Boil the water
  8. Brew (); //Brew with boiling water ...
  9. Pourincup (); //Put ... Pour into the cup
  10. //Hooks decide whether to add ingredients
  11. if (Hook ()) {
  12. Addcoundiments (); //Plus ...
  13. }
  14. //hook ();
  15. }
  16. /** 
  17. * Add Ingredients by default
  18. * @return
  19. */
  20. Public Boolean hook () {
  21. return true;
  22. }
  23. //public void Hook () {}
  24. public abstract void addcoundiments ();
  25. public abstract Void Brew ();
  26. public void Boilwater () {
  27. System.out.println ("boiled water");
  28. }
  29. public void Pourincup () {
  30. System.out.println ("pour into cup");
  31. }
  32. }

2> We modify the coffee class so that it does not add ingredients

Java code
  1. Package com.kaishengit.beverage;
  2. Public class Coffee extends beverage{
  3. @Override
  4. public void Addcoundiments () {
  5. System.out.println ("Add sugar and milk"); }  
  6. @Override
  7. public Void Brew () {
  8. System.out.println ("Brew Coffee with water");
  9. }
  10. /** 
  11. * Some guests do not like to add ingredients
  12. */
  13. @Override
  14. Public Boolean hook () {
  15. return false;
  16. }
  17. / * @Override
  18. public void Hook () {
  19. System.out.println ("One more Cup");
  20. }
  21. */
  22. }

3> or use the test class above

Operation Result:

------------------------------------------------------

Boiled boiled water

Coffee with water

Pour into the cup

------------------------------------------------------

No ingredients added in running results

About template mode

The 1> template pattern defines the steps of the algorithm, delaying the implementation of these steps to subclasses

2> template mode provides us with a code reuse technique.

3> Template Abstract classes can define specific methods, abstract methods, and Hook methods

4> to prevent subclasses from changing the algorithm in the template, the template method can be declared as final

A 5> hook is a method that does not work in an abstract class, or only does the default, and subclasses can choose to implement it

Template Pattern Pattern method

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.